gpt4 book ai didi

选择/加入/组上的 Postgresql 错误语法

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

以下是 Postgresql 的不正确语法吗?

select p.*, SUM(vote) as votes_count 
FROM votes v, posts p
where p.id = v.`voteable_id`
AND v.`voteable_type` = 'Post'
group by v.voteable_id
order by votes_count DESC limit 20

我正在本地安装 postgresql,但想尽快完成它:)

谢谢

最佳答案

MySQL 对标准 SQL 的解释比 PostgreSQL 宽松得多。您的查询有两个问题:

  1. 反引号是 MySQL 的东西。
  2. 您的GROUP BY 无效。

第一个可以通过简单地删除有问题的引号来修复。第二个需要更多的工作;来自 fine manual :

When GROUP BY is present, it is not valid for the SELECT list expressions to refer to ungrouped columns except within aggregate functions, since there would be more than one possible value to return for an ungrouped column.

这意味着您的 SELECT 中提到的每一列要么出现在聚合函数中,要么出现在 GROUP BY 子句中。所以,你必须扩展你的 p.* 并确保所有这些列都在 GROUP BY 中,你应该以这样的方式结束,但真正的列在p.column... 的位置:

select p.id, p.column..., sum(v.vote) as votes_count
from votes v, posts p
where p.id = v.voteable_id
and v.voteable_type = 'Post'
group by p.id, p.column...
order by votes_count desc
limit 20

从 MySQL 迁移到其他任何平台时,这是一个很常见的问题。

关于选择/加入/组上的 Postgresql 错误语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5502847/

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