I have a table of products, there can be multiple products with the same name but with different prices, i want to display only the cheapest price of each product.
Example Of Table:
TABLE: Products(name,price)
---------------------------
Banana -- 1,25
Banana -- 1,10
Strawberry -- 2,43
Apple -- 1,11
Apple -- 4,12
Apple -- // Some Products Can Have the Price Column Empty!!
I want a MySQL query to get this:
- Banana From: $1,10
- Strawberry From: $2,43
- Apple From: $1,11
Thanks!
Off the top of my head, something like
select name, min(price) from products group by name;
See this link for more info.
http://www.techonthenet.com/sql/group_by.php
Then use PHP to display the data however you wish e.g. in a table etc.
Also, you haven’t said whether the price field can be NULL or not.
You also haven’t said whether your name field could have mixed case like “bananas” or “Bananas” or similar. If so then you’ll probably want to call UPPER(TRIM(name)) or something like that on the name field to normalize it.