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%'");
// fol开发者_Go百科low by while loop for echoing
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.
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%'");
精彩评论