What I have are two tables:
mark_notifications
mark_notifications_options
The mark_notifications table contains rows of data that include a column tid and userid
The mark_notifications_options table contains a special column grouptogether
What I am trying to achieve is that if the userid’s mark_notifications_options.grouptogether value is set to 0, it will add up every row that matches his userid in the mark_notifications table
However, if the userid’s mark_notifications_options.grouptogether value is set to 1, it will add up every row that matches his userid where all the tids are grouped, so that if there no matter how many times the same tid value comes up, it only gets counted as 1. I also would like to count the times tid = 0 as a separate entitiy (but NOT grouped) and then I would just add the values later in php.
This is what I have come up with so far, but the resulting values dont make any sense at all, im getting results that number in the thousands (when it should be a low number like 8 for example).
SELECT
mark_notifications_options.grouptogether,
COUNT(mark_notifications.threadid) AS ungrouped,
SUM(mark_notifications.threadid = 0) AS total_zero,
COUNT(DISTINCT mark_notifications.threadid) AS grouped
FROM mark_notifications, mark_notifications_options
WHERE mark_notifications.userid = $userid
GROUP BY mark_notifications_options.grouptogether
Thanks!!!
You were missing a join predicate, and so were getting the cartesian product (ie resulting in n x n rows).
Try this: