1

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

2
  • write your query and just make sure that WHERE clause comes first Commented May 4, 2010 at 11:00
  • is it the very first SQL query you're going to execute in PHP? Commented May 4, 2010 at 11:14

6 Answers 6

3
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());
Sign up to request clarification or add additional context in comments.

1 Comment

select * from table_name GROUP BY id_product ORDER BY (select t.value FROM table_name t WHERE name='price' AND t.id_product = table_name.id_product)
3
SELECT * 
FROM table_name 
WHERE name = 'price'
GROUP BY id_product
ORDER BY value

You can see this for MySQL Select syntax

Comments

2

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

Comments

1

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

Comments

1

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.

Comments

0

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
)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.