Postgresql select/join/group 上的错误语法
下面的内容对于 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,但想尽快解决这个问题:)
谢谢
What about the following is not proper syntax for 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
I am in the process of installing postgresql locally but wanted to get this out sooner :)
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
MySQL 对标准 SQL 的解释比 PostgreSQL 宽松得多。您的查询有两个问题:
GROUP BY
无效。第一个问题可以通过简单地删除有问题的引号来修复。第二个需要更多的工作;来自精细手册:
这意味着
SELECT
中提到的每一列都必须出现在聚合函数或GROUP BY
子句中。因此,您必须扩展您的p.*
并确保所有这些列都在GROUP BY
中,您最终应该得到类似这样的结果,但真正的列在place ofp.column...
:从 MySQL 迁移到其他任何东西时,这是一个非常常见的问题。
MySQL is a lot looser in its interpretation of standard SQL than PostgreSQL is. There are two issues with your query:
GROUP BY
is invalid.The first one can be fixed by simply removing the offending quotes. The second one requires more work; from the fine manual:
This means that every column mentioned in your
SELECT
either has to appear in an aggregate function or in theGROUP BY
clause. So, you have to expand yourp.*
and make sure that all those columns are in theGROUP BY
, you should end up with something like this but with real columns in place ofp.column...
:This is a pretty common problem when moving from MySQL to anything else.