gpt4 book ai didi

mysql - PostgreSQL 上的 Doctrine SQL 查询 : works on MySQL, 不会

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

这是由 Doctrine ORM 的 Taggable 扩展自动生成的查询。

SELECT t.id AS t__id, t.name AS t__name, COUNT(DISTINCT i.id) AS i__0,
(COUNT(DISTINCT i.id)) AS i__1
FROM taggable_tag t
LEFT JOIN cms__model__image_taggable_tag c ON (t.id = c.tag_id)
LEFT JOIN image i ON i.id = c.id
WHERE t.id IN
(SELECT doctrine_subquery_alias.id
FROM
(SELECT DISTINCT t2.id, (COUNT(DISTINCT i2.id)) AS i2__1
FROM taggable_tag t2
LEFT JOIN cms__model__image_taggable_tag c2 ON (t2.id = c2.tag_id)
LEFT JOIN image i2 ON i2.id = c2.id
GROUP BY t2.id HAVING i2__1 > 0
ORDER BY i2__1 DESC LIMIT 10) AS doctrine_subquery_alias)
GROUP BY t.id HAVING i__1 > 0
ORDER BY i__1 DESC

它在使用 MySql 时有效,但不适用于 PostgreSql。

我得到:column i2__1 not foundcolumn i__2 not found

使用 COUNT(DISTINCT) 时是否不允许使用别名?
此查询在 PostgreSql 上应该如何工作?

最佳答案

您可以尝试在子选择的 HAVING 子句中将 i2__1 替换为 COUNT(DISTINCT i2.id),或者删除 COUNT(DISTINCT i2.id) 两边的括号。

您可能还必须将 t__name 添加到主选择的 GROUP BY 子句中。

关于mysql - PostgreSQL 上的 Doctrine SQL 查询 : works on MySQL, 不会,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2647537/

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