简体   繁体   中英

MySQL count rows with same values

i have been searching but i didnt find what i was looking for. This is what i have:

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%

it bring what i needed: I needed a query that shows me the email of an user and how many topics and replys he made (BBPRESS).

The problem is that it brings all of them but it doesnt tell me how many topics / replies the user made. This is what it brings: http://i.stack.imgur.com/sMymU.png Is there any way to add a 3rd column where it counts?

Conditional aggregation:

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

Try this:

  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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM