gpt4 book ai didi

mysql - 内连接 SQL ORDER BY 不起作用

转载 作者:行者123 更新时间:2023-11-29 10:29:24 26 4
gpt4 key购买 nike

工作!

SELECT s.*, COUNT(rs.id_segreto) as tot
FROM Segreti s INNER JOIN
Reazioni_Segreti rs
ON s.id = rs.id_segreto
WHERE rs.tipo_reazione = 'mi_piace'
GROUP BY s.id
ORDER BY tot DESC
LIMIT 10;

但它只打印 1 行。

$rs_result = mysql_query($sql);
while ($row = mysql_fetch_array($rs_result, MYSQL_ASSOC)) {
echo $row['id']."<br>";
}

为什么会发生?我错了什么?

最佳答案

这是 MySQL 允许的语法,但 SQL 标准不允许——其他数据库也不允许。 SELECT 中有大量未分组的字段,但 COUNT() 将其转换为聚合查询。

如果没有group by,这样的查询总是返回一行。

我还建议使用表别名:

SELECT s.*, COUNT(rs.id_segreto) as tot
FROM Segreti s INNER JOIN
Reazioni_Segreti rs
ON s.id = rs.id_segreto
WHERE rs.tipo_reazione = 'mi_piace'
GROUP BY s.id
ORDER BY tot DESC
LIMIT 10;

请注意,在 GROUP BY 中使用 s.id(而不是列出所有列)是允许的 - 即使在 ANSI 下 - 假设 id 被定义为表中唯一的。

关于mysql - 内连接 SQL ORDER BY 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47699171/

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