I have two tables in my database, one for In and one for Out . They have two columns, Quantity and Price . How can I write an SQL query that selects the correct price?
In the example: if I have 3 elements in for 75, and then 3 elements in for 80. Then I have two out for 75, and the third out should be for 75 (X), and the fourth out should be for 80 (Y )
How can I write a price request for X and Y? They should use the price from the third and fourth row. In the example, is there a way to SELECT the third row in the In -table? I can not use auto_increment as an identifier for the row "third", because in the tables a message will also be displayed for other elements. Lines will not be deleted, they will be saved to explain the reasons.
SELECT Price FROM In WHERE ...?
NEW :
+----+ | In | +----+------+-------+ | Supply_ID | Price | +-----------+-------+ | 1 | 75 | | 1 | 75 | | 1 | 75 | | 2 | 80 | | 2 | 80 | +-----------+-------+ +-----+ | Out | +-----+-------+-------+ | Delivery_ID | Price | +-------------+-------+ | 1 | 75 | | 1 | 75 | | 2 | X | <- ? | 3 | Y | <- ? +-------------+-------+
OLD :
+----+ | In | +----+------+----------+-------+ | Supply_ID | Quantity | Price | +-----------+----------+-------+ | 1 | 3 | 75 | | 2 | 3 | 80 | +-----------+----------+-------+ +-----+ | Out | +-----+-------+----------+-------+ | Delivery_ID | Quantity | Price | +-------------+----------+-------+ | 1 | 2 | 75 | | 2 | 1 | X | <- ? | 3 | 1 | Y | <- ? +-------------+----------+-------+
Jonas source share