tags:

views:

54

answers:

5
+1  Q: 

php, sql selection

I have a stupid question, I have this table :

id_product    name        value
   1          price       10-20
   1          type        computer
   2          price       20-30
   3          price       100-200

and I want to select from this table GROUP BY id_product and ORDER BY value WHERE name='price'

how can i do this?

Thanks a lot

+3  A: 
SELECT * 
FROM table_name 
WHERE name = 'price'
GROUP BY id_product
ORDER BY value

You can see this for MySQL Select syntax

codaddict
+2  A: 

select * from table_name where name='price' Group By id_product ORDER By value;

Nitz
+3  A: 
select * from table_name  WHERE name='price' 
    GROUP BY id_product
    ORDER BY value

In PHP

mysql_query("select * from table_name  WHERE name='price' 
        GROUP BY id_product
        ORDER BY value") or die(mysql_error());
Salil
in case he's using mysql
Col. Shrapnel
+1  A: 

right off the top of my head...

does it work?

SELECT * FROM `table` WHERE `table`.`name`='price' GROUP BY `table`.id_product ORDER BY `table`.`value` ASC
Glycerine
+1  A: 

It depends on how you want to group items with the same id: which one do you want to be shown? In theory GROUP BY shouldn't be used with SELECT * (it does not work with non mysql databases) and should be associated with aggregate funcions such as COUNT(), MAX(), etc.

You might just need SELECT DISTINCT instead of GROUP BY.

kemp