简体   繁体   English

获取多个子查询结果的总和

[英]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). 我的查询选择与用户ID匹配的所有标签,然后针对每个标签,检查另一个特定用户是否具有相同标签(用户具有多个标签)。 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. 输出是标签列表,如果有匹配项,则“ count”为1,否则为0。 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). 所有信息都在那里,但我想获得所有匹配项的总和(在本例中为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: 我最终使用以下查询来获取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). 我仍然无法在另一个查询中包装此计数查询(最终得到所有用户ID和相应计数的列表)。 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). 我最终使用了Hang的答案,因为它只对当前查询进行了最小的更改,而且我找不到使用Gordon Linoff的答案获得相同结果的方法(由于我缺乏理解)。 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. 我想“从用户中选择ID”,并为每个用户使用更新的子查询来获取计数,将“ 102”替换为每个用户的ID。 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. 如果我理解正确,那么您希望用户101和用户102也具有的标签计数。 You can do this with a join : 您可以通过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) . 如果存在,则使用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包裹在查询之外,如下所示:

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'
     )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM