Can you pass variables into subqueries, like so?
SELECT users.id AS id,
(SELECT SUM(total) FROM (image_totals) WHERE `cat_id` IN ('5', '3') AND `user_id` =id) AS image_count
This is only part of a larger query, all being generated through Active Record, but the key issue remains, the ‘id’ variable, how can I pass this into my subquery? Am I looking at this all wrong?
Thanks in advance. And let me know if you need more info.
Use GROUP BY. You want SUM(total) for each user.id for particular values of cat_id. I don’t know what the rest of your query looks like, but for what was shown, you can just: