tonoslfx
tonoslfx

Reputation: 3442

mysql GROUP BY products

table:
id   p_name      p_price
1    tan           1
2    soap          1
3    pro tan 01    2 
4    pro tan 02    3 
5    pro tan 03    4

How do I group the products that contain pro tan, something what I want for the html result

product name     price
tan                1
soap               1
pro tan            9

php:

$q_products = $db->query("SELECT * FROM p_products 
                          GROUP BY p_name 
                          HAVING LIKE 'pro tan%'"); 
// follow by while loop for echoing

Upvotes: 0

Views: 224

Answers (2)

Frank Schmitt
Frank Schmitt

Reputation: 30775

select s.p_name, sum(s.p_price) 
from 
  (select left(p_name, 7) as p_name, 
   p_price from products) s
group by s.p_name;

should do what you want.

Upvotes: 4

Eon
Eon

Reputation: 3974

OK, I cant comment yet, but try this

$q_products = $db->query("SELECT * FROM p_products GROUP BY p_name having p_name LIKE 'pro tan%'");

Upvotes: 0

Related Questions