1

我有一个数据库,其中有 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 

但每一行都算我

先感谢您!

4

4 回答 4

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 

编辑:substring(new_name,0,5)来自 OP

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 
于 2013-07-26T08:35:24.137 回答
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
于 2013-07-26T08:35:00.963 回答
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 
于 2013-07-26T08:38:17.513 回答
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

效果更好,我做到了,因为您的代码工作错误对不起,如果我没有告诉您更多信息。

谢谢)

于 2013-07-26T11:51:12.213 回答