gpt4 book ai didi

mysql - SQL 计算所有行而不是计算单个行

转载 作者:行者123 更新时间:2023-11-29 20:27:15 25 4
gpt4 key购买 nike

我有一个从数据库请求数据的 SQL 语句。

SELECT `ID`, `To`, `Poster`, `Content`, `Time`, ifnull(`Aura`,0) as `Aura` FROM (
SELECT * FROM (
SELECT DISTINCT * FROM messages m
INNER JOIN
(
SELECT Friend2 as Friend FROM friends WHERE Friend1 = '1'
UNION ALL
SELECT Friend1 as Friend FROM friends WHERE Friend2 = '1'
) friends ON m.Poster = friends.`Friend`
UNION ALL SELECT DISTINCT *, '1' FROM messages where `Poster`='1'
) var
LEFT JOIN
(
select `ID` as `AuraID`, `Status` as `AuraStatus`, count(*) as `Aura`
from messages_aura
) aura ON (var.Poster = aura.AuraID AND var.ID = aura.AuraStatus)
) final

GROUP BY `ID`, `Poster`
ORDER BY `Time` DESC LIMIT 10

这是我的 messages_aura 表格布局。它显示 IDStatusUserID

这是上述语句的输出。

(上面截图中的ID指的是下面的Poster,上面截图中的Status指的是ID如下)

该语句应为底行指定 Aura 计数 1,为顶行指定 Aura 计数 2。怎么了?

最佳答案

您缺少 GROUP BY,因此它会计算所有内容,而不是按某些列进行分组。

LEFT JOIN
(
select `ID` as `AuraID`, `Status` as `AuraStatus`, count(*) as `Aura`
from messages_aura
GROUP BY AuraID, AuraStatus
) aura ON (var.Poster = aura.AuraID AND var.ID = aura.AuraStatus)

关于mysql - SQL 计算所有行而不是计算单个行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39234959/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com