mysql - Express引擎 : Custom query - Grouping entries daily

标签 mysql join group-by

我有以下查询,该查询对每天的条目进行分组和计数:

SELECT DISTINCT `year`, `month`, `day`, count(entry_id) as 'posts'
FROM (`exp_channel_titles`)
WHERE `status` =  'open'
GROUP BY `year`, `month`, `day`
ORDER BY `entry_date` desc  

它工作正常,但它从所有类别中进行选择,但我想限制为一个或多个类别。

对于不熟悉 EE 数据库的人,这里是我们可能需要的一些表格示例

exp_channel_titles:

entry_id year month day
1 2011 11 5
2 2011 11 6
3 2011 11 7

exp_categories:

cat_id cat_name
1 sport
2 computer
3 car

exp_category_posts:将条目连接到类别

entry_id cat_id
1 2
1 2
2 1
3 3

最佳答案

SELECT ect.year, ect.month, ect.day, count(ect.entry_id) as posts
FROM exp_channel_titles as ect
INNER JOIN exp_category_posts ecp
  ON ecp.entry_id=ect.entry_id
INNER JOIN exp_categories ec
  ON ecp.cat_id=ec.cat_id
WHERE ect.status = 'open' 
  AND ec.cat_name = "computer" /* this is to filter by category name */
GROUP BY ect.year, ect.month, ect.day
ORDER BY ect.entry_date desc;  /* where is this entry_date come from? */

关于mysql - Express引擎 : Custom query - Grouping entries daily,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8465912/

相关文章:

mysql - SHOW 上下文中的 FROM 与 IN

javascript - 仅连接并打印带有文本的值,并在之间添加逗号

Mysql: `natural join`如何使用 `join_condition`?

sql - 查找每个 GROUP BY 结果中的行数

r - 在大 df 上进行 Groupby 或聚合

php - 试图避免 sql 注入(inject)攻击 - 导致数据未被插入

MySQL 导入在 Mac 上的 Vagrant CoreOS 盒子上挂起

php - 检索按成员分组的每日竞赛条目

php - 如果图像数组为空,则显示替代图像

php - 复杂的 Mysql 数据库连接