简体   繁体   中英

Get SUM of multiple subquery results

My query selects all tags that match a user's id, and then for each tag, checks if another specific user has the same tag(users have multiple tags). Here is what I currently have:

SELECT name as tname,
(
SELECT COUNT(*) FROM tags WHERE tags.name = tname AND user_id = '101'
) as count
FROM tags
WHERE user_id = '102'

The output is a list of tags, with 'count' as 1 if there is a match, or 0 if there is no match. It looks like this:

+--------+-------+
| tname  | count |
+--------+-------+
| Apple  |     1 |
| Banana |     1 |
| Orange |     1 |
| Peach  |     0 |
| Pear   |     1 |
+--------+-------+

All the information is there, but I want to get a SUM of all the matches (in this case it would be 4). I will then use all this inside another query so that I can have a query like this:

SELECT sum( count ) as total_count, class FROM table where count > 3 GROUP BY class

Any help greatly appreciated!

UPDATE:

I ended up using the following query to get the sum of 4:

SELECT SUM(count) as count
FROM (
     SELECT name as tname,
     (
     SELECT COUNT(*) FROM tags WHERE tags.name = tname AND user_id = '101'
     ) as count
     FROM tags
     WHERE user_id = '102'
     ) as t

This is great, but it is only the first half of what I wanted to achieve. I'm still having trouble wrapping this count query inside another query (to end up with a list of all user IDs and corresponding count). I ended up using Hang's answer because it involved minimal changes to my current query and I couldn't find a way to get the same result using Gordon Linoff's answer (due to my lack of understanding). My desired result would look something like this:

+--------+-------+
| id     | count |
+--------+-------+
| 102    |     4 |
| 103    |     3 |
| 104    |     7 |
| 105    |     2 |
| 106    |     4 |
+--------+-------+

I want to 'SELECT id FROM users', and for each user, get the count by using the updated subquery, replacing the '102' with the id for each user. If what I'm saying needs clarification, please ask! Thanks again for all your help!

If I understand correctly, you want the count of tags that user 101 has that user 102 also has. You can do this with a join :

select count(*)
from tags t101 join
     tags t102
     on t101.tag = t102.tag and
        t101.user_id = 101 and
        t102.user_id = 102;

This assumes that there are no duplicates in the table. If there are, then use select count(distinct t101.tag) .

By the way, you can get the matching tags using a slight variation on the same query:

select t101.tag, count(t102.user_id)
from tags t101 left join
     tags t102
     on t101.tag = t102.tag and
        t102.user_id = 102
where t101.user_id = 101
group by t101.tag;

Wrap a SELECT outside your query like this:

SELECT SUM(count)
FROM (
     SELECT name as tname,
     (
     SELECT COUNT(*) FROM tags WHERE tags.name = tname AND user_id = '101'
     ) as count
     FROM tags
     WHERE user_id = '102'
     )

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