gpt4 book ai didi

SQL 错误 : misuse of aggregate

转载 作者:IT王子 更新时间:2023-10-29 06:17:37 31 4
gpt4 key购买 nike

SQLite 版本 3.4.0聚合函数有什么问题?此外,我怀疑 ORDER BY 也不会起作用。如何重写这个?

sqlite> SELECT p1.domain_id, p2.domain_id, COUNT(p1.domain_id) AS d1, COUNT(p2.domain_id) AS d2
...> FROM PDB as p1, Interacting_PDBs as i1, PDB as p2, Interacting_PDBs as i2
...> WHERE p1.id = i1.PDB_first_id
...> AND p2.id = i2.PDB_second_id
...> AND i1.id = i2.id
...> AND d1>100
...> AND d2>100
...> ORDER BY d1, d2;
SQL error: misuse of aggregate:
sqlite>

最佳答案

当使用聚合函数(sum/count/...)时,您还必须使用 GROUP BY 子句。

此外,当你想过滤聚合的结果时,你不能在 WHERE 子句中这样做,但你必须在 HAVING 子句中这样做。

SELECT p1.domain_id, p2.domain_id, COUNT(p1.domain_id) AS d1, COUNT(p2.domain_id) AS d2
FROM PDB as p1, Interacting_PDBs as i1, PDB as p2, Interacting_PDBs as i2
WHERE p1.id = i1.PDB_first_id
AND p2.id = i2.PDB_second_id
AND i1.id = i2.id
GROUP BY p1.domain_Id, p2.domain_Id
HAVING d1 > 100 AND d2 > 100
ORDER BY d1, d2;

关于SQL 错误 : misuse of aggregate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/648083/

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