cosy
cosy

Reputation: 582

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

Upvotes: 1

Views: 93

Answers (6)

imclean
imclean

Reputation: 349

This will select distinct id_product from table_name and order by value where name is "price". I think this will give the outcome you're after.

SELECT DISTINCT id_product 
FROM table_name 
ORDER BY (
    SELECT t.value FROM table_name t 
    WHERE t.name='price'
    AND t.id_product = table_name.id_product
)

Upvotes: 0

Matteo Riva
Matteo Riva

Reputation: 25060

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.

Upvotes: 1

Glycerine
Glycerine

Reputation: 7347

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

Upvotes: 1

Salil
Salil

Reputation: 47542

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());

Upvotes: 3

Nitz
Nitz

Reputation: 1726

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

Upvotes: 2

codaddict
codaddict

Reputation: 455440

SELECT * 
FROM table_name 
WHERE name = 'price'
GROUP BY id_product
ORDER BY value

You can see this for MySQL Select syntax

Upvotes: 3

Related Questions