我一直在寻找但我找不到我想要的东西。 这就是我所拥有的:
SELECT user_email, post_type
FROM `wp_users`
INNER JOIN `wp_posts` ON wp_users.id = wp_posts.post_author
WHERE post_type LIKE '%topic%'
OR post_type LIKE '%reply%
它带来了我需要的东西: 我需要一个查询,向我显示用户的电子邮件以及他做了多少主题和回复(BBPRESS)。
问题在于它带来了所有这些但它并没有告诉我用户做了多少主题/回复。 这就是它带来的: http://i.stack.imgur.com/sMymU.png 有没有办法在它重要的地方添加第3列?
答案 0 :(得分:1)
条件聚合:
SELECT user_email,
SUM(CASE WHEN post_type LIKE '%reply%' THEN 1 ELSE 0 END) AS Replies,
SUM(CASE WHEN post_type LIKE '%topic%' THEN 1 ELSE 0 END) AS Topics
FROM `wp_users`
INNER JOIN `wp_posts` ON wp_users.id = wp_posts.post_author
WHERE post_type LIKE '%topic%' OR post_type LIKE '%reply%'
GROUP BY user_mail
答案 1 :(得分:0)
试试这个:
SELECT user_email,
SUM(CASE WHEN post_type LIKE '%topic%' THEN 1 ELSE 0 END) AS Topics,
SUM(CASE WHEN post_type LIKE '%reply%' THEN 1 ELSE 0 END ) AS Replies
FROM `wp_users`
INNER JOIN `wp_posts` ON wp_users.id = wp_posts.post_author
GROUP BY user_email