数数和分组

时间:2014-12-01 22:11:05

标签: mysql group-by

我有两张桌子:

exp_channel_titles T (parent table)
exp_category_posts P (many to many table that links to T via column entry_id)

我需要知道每个author_id个类别中有多少条目使用来自T author_id的{​​{1}},entry_id字段。

e.g。

T.channel_id = 7

1 个答案:

答案 0 :(得分:1)

试试这个:

SELECT 
    p.cat_id, 
    t.author_id,
    count(*) as cat_entries
FROM 
    exp_channel_titles t
    join exp_category_posts p on t.entry_id = p.entry_id
WHERE
    t.channel_id = 7
GROUP BY
    p.cat_id,
    t.author_id