我有一个数据库,我有id,new_name,product_id,date
1162 DC: 10us 1049902 2013-07-19
1163 DC: 12us 1049902 2013-07-19
1164 DC: 30us 1049902 2013-07-19
1165 Top 1049902 2017-07-30
1166 A:123 202302 2013-07-21
1167 A:255 2023025 2013-07-21
我需要选择日期相等的行(例如每行的日期= 2013-07-19,每行的product_id =(例如)1049902)并将这些行计为1行
这样:
DC 1049902 = 2 (because there are two different dates that's why 2 besause it is summary (the names are differenet it doesn't matter)
A 202302 = 1
A 2023025 = 1
(在查询后我只是substr()字符串)
依旧......
我试着这样做:
select new_name, COUNT(new_name) AS n from table WHERE date<='".$today."' AND date>= '".$two_weeks_ago."' GROUP BY day(date) ORDER BY n DESC
但它每一行都算我
提前谢谢!
答案 0 :(得分:1)
尝试这种方式:
select new_name, product_id,COUNT(day(date)) AS n
from table
WHERE date<='".$today."' AND date>= '".$two_weeks_ago."'
GROUP BY new_name, product_id
ORDER BY n DESC
编辑:
来自OP的substring(new_name,0,5)
select substring(new_name,0,5) as new_name, product_id,COUNT(day(date)) AS n
from table
WHERE date<='".$today."' AND date>= '".$two_weeks_ago."'
GROUP BY substring(new_name,0,5), product_id
ORDER BY n DESC
答案 1 :(得分:0)
按product_id和new_name分组
SELECT new_name, COUNT(new_name) AS n
FROM table
WHERE date<='".$today."' AND date>= '".$two_weeks_ago."'
GROUP BY new_name,product_id
ORDER BY n DESC
答案 2 :(得分:0)
使用此查询:
SELECT `new_name`, COUNT(`new_name`) AS n
FROM table
WHERE `date`<='".$today."' AND `date` >= '".$two_weeks_ago."'
GROUP BY `product_id`, `date`
ORDER BY n DESC
答案 3 :(得分:0)
@Parado,请查看此代码:
select brand_name, count(*) AS num
from (select SUBSTRING_INDEX(new_name, ':', 1) as brand_name
from table
WHERE date<='".$american_today."' AND date>= '".$two_weeks_ago."'
GROUP BY day(date), products_id)
as brands group by brand_name ORDER BY num DESC LIMIT 5
这项工作做得更好,我做到了,因为你的代码工作错了 对不起,如果我没有告诉你更多信息。
感谢)