I’m having problems with this query. It returns correct total_names, total_events, and total_misc, but the other three totals (pending names, events, and misc) are the same number and totally the wrong number. All three tables have the created_by column. What am I doing wrong?
SELECT
COUNT(DISTINCT names_revisions.id) AS total_names,
COUNT(DISTINCT events_revisions.id) AS total_events,
COUNT(DISTINCT misc_revisions.id) AS total_misc,
SUM(if(names_revisions.status = "Pending", 1, 0)) AS total_pending_names,
SUM(if(events_revisions.status = "Pending", 1, 0)) AS total_pending_events,
SUM(if(misc_revisions.status = "Pending", 1, 0)) AS total_pending_misc
FROM
names_revisions,
events_revisions,
misc_revisions
WHERE
:user_id IN (names_revisions.created_by, events_revisions.created_by, misc_revisions.created_by)
You are joining the tables instead of just selecting the counts separately. It will be faster to use separate queries for the 3 tables instead of joins