开发者

mysql GROUP BY products

开发者 https://www.devze.com 2023-02-26 16:05 出处:网络
table: idp_namep_price 1tan1 2soap1 3pro tan 012 4pro tan 023 5pro tan 034 How do I group the products that contain pro tan, something what I want for the html result
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%'");
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号