0

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: https://i.sstatic.net/sMymU.png Is there any way to add a 3rd column where it counts?

1
  • You can group by user_email and do a count on the post_type column. Commented Jun 15, 2015 at 17:35

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.