mysql GROUP BY产品

时间:2011-04-19 12:28:53

标签: php mysql group-by

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

如何对包含pro tan的产品进行分组,这是我想要的html结果

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

2 个答案:

答案 0 :(得分:4)

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;

应该做你想做的事。

答案 1 :(得分:0)

好的,我还不能评论,但试试这个

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