简体   繁体   English

如何获得 1 行结果而不是 2

[英]How to get 1 row result instead of 2

I'm trying to grab 2 count values from a union all query and then combine the sum of those 2 values so i then have a total count.我试图从 union all 查询中获取 2 个计数值,然后组合这 2 个值的总和,这样我就有了一个总计数。 The query at the moment is giving me 2 rows of data which is 1 for each of the queries.目前的查询给了我 2 行数据,每个查询为 1。

How can i update the below query to only display 1 row with the total count of the 2 queries.如何更新以下查询以仅显示 1 行,其中包含 2 个查询的总数。

SELECT SUM(qty) as qty
FROM (
    SELECT COUNT(f.unread) as qty
    FROM users u, followers f
    WHERE u.uid = f.follower AND f.uid = '605bb0e3d8fb16.55214369' AND f.unread = 'Y' GROUP BY f.unread
    UNION ALL
    SELECT COUNT(f.unread) as qty
    FROM users u, comments f, subs s
    WHERE u.uid = f.uid AND s.sid = f.sid AND s.uid = '605bb0e3d8fb16.55214369' AND f.uid <> '605bb0e3d8fb16.55214369' AND f.unread = 'Y' GROUP BY f.unread
) t
GROUP BY t.qty

Thanks in advance提前致谢

try not to use the old尽量不要使用旧的

select something
from table1, table2, table3
where table1.something = table2.something

Instead list your joins properly:而是正确列出您的连接:

SELECT SUM(qty) as qty
FROM (
    SELECT COUNT(f.unread) as qty
    FROM users u
    JOIN followers f ON u.uid = f.follower f.uid = '605bb0e3d8fb16.55214369' AND f.unread = 'Y'
    UNION ALL
    SELECT COUNT(f.unread) as qty
    FROM users u
    JOIN comments f ON u.uid = f.uid AND f.uid <> '605bb0e3d8fb16.55214369' AND f.unread = 'Y' 
    JOIN subs s ON s.sid = f.sid AND s.uid = '605bb0e3d8fb16.55214369'
) t

Once you get used to it the new method is a lot easier to follow especially in larger queries一旦习惯了新方法,就会更容易遵循,尤其是在较大的查询中

You can move the non table link clauses ie AND s.uid = '605bb0e3d8fb16.55214369' out to a where clause at the end if you like but I like to keep anything relating to a table set for joining purposes in the join itself for readability.如果您愿意,您可以将非表链接子句,即AND s.uid = '605bb0e3d8fb16.55214369'移到最后的 where 子句中,如果您愿意,但我喜欢保留与表集相关的任何内容,以便在连接本身中进行连接以提高可读性. It's discussed a bit here: Condition within JOIN or WHERE and keeping the filter in the join can give a perfomance boost but I am not sure how much of a boost we are talking, I just find it easier to read and maintain this way.这里讨论了一点: 在 JOIN 或 WHERE 中设置条件并将过滤器保持在连接中可以提高性能,但我不确定我们所说的提升有多大,我只是发现这种方式更容易阅读和维护。

When there is no GROUP BY, aggregate functions such as SUM work on all rows.当没有 GROUP BY 时,诸如 SUM 之类的聚合函数适用于所有行。 So you simply need to remove the outer GROUP BY to sum the results of the two queries.因此,您只需删除外部 GROUP BY 即可对两个查询的结果求和。

The inner GROUP BY clauses are also unneeded, though not harmful, since there is only one possible value for f.unread in each of the two unioned queries.内部的 GROUP BY 子句也是不需要的,但没有害处,因为在两个联合查询中的每一个中, f.unread 只有一个可能的值。

Alternatively, you can simply add the results instead of using SUM/UNION:或者,您可以简单地添加结果而不是使用 SUM/UNION:

SELECT (
    SELECT COUNT(f.unread) as qty
    FROM users u, followers f
    WHERE u.uid = f.follower AND f.uid = '605bb0e3d8fb16.55214369' AND f.unread = 'Y'
) + (
    SELECT COUNT(f.unread) as qty
    FROM users u, comments f, subs s
    WHERE u.uid = f.uid AND s.sid = f.sid AND s.uid = '605bb0e3d8fb16.55214369' AND f.uid <> '605bb0e3d8fb16.55214369' AND f.unread = 'Y'
);

since you can use a parenthesized subquery in an expression as long as it returns only one column and one row.因为您可以在表达式中使用带括号的子查询,只要它只返回一列和一行。

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

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