I have two tables info
and sell_info
. info
table contains "list of record" and sell_info
contains "product which are on sale".
I want to get "lowest" value of record where id = 1
. How can i do this?
Here is my table "info":
id | name |
---|---|
1 | ABC |
2 | XYZ |
3 | CDE |
… | … |
Here is my table "sell_info":
id | product_id | price |
---|---|---|
1 | 1 | 5 |
2 | 1 | 3 |
3 | 1 | 8 |
4 | 2 | 2 |
… | … | … |
Expected result is (getting lowest price of id="1"):
id | name | price |
---|---|---|
1 | ABC | 3 |
3
Answers
The following query would give you what you want.
It uses an inner query to get the minimum price for each product ID from the
SELL_INFO
table. It then uses the product ID from that inner query to join to theINFO
table, to get the name of the product.This should work
It’s quite easy.
Here is a SQLFIDDLE.