【发布时间】:2017-12-11 23:35:52
【问题描述】:
每个人。
我正在使用 grails 3.3.0.M2 框架和 mysql 作为数据源,以下 sql 查询按预期工作
SELECT
c.name,
SUM(CASE
WHEN t.status = 'open' THEN 1
ELSE 0
END) 'open',
SUM(CASE
WHEN t.status = 'pending' THEN 1
ELSE 0
END) 'in progress',
SUM(CASE
WHEN t.status = 'closed' THEN 1
ELSE 0
END) 'closed'
FROM
tickets t
INNER JOIN
users u ON t.user_id = u.id
INNER JOIN
user_coordinations uc ON uc.user_id = u.id
INNER JOIN
coordinations c ON c.id = uc.coordination_id
GROUP BY 1
我使用隐式 JOIN 翻译成 HQL 但我得到了错误的结果,这里是 hql 查询:
SELECT
c.name,
SUM(CASE
WHEN t.status = 'open' THEN 1
ELSE 0
END),
SUM(CASE
WHEN t.status = 'pending' THEN 1
ELSE 0
END),
SUM(CASE
WHEN t.status = 'closed' THEN 1
ELSE 0
END)
FROM
Ticket t, User u, UserCoordination uc, Coordination c
WHERE
MONTH(t.dateCreated) = :month
GROUP BY 1
为了得到正确的结果堆栈溢出用户帮助我理解查询需要使用显式连接,这里的问题是:Group by a field that does not belongs to the consulted table
现在我正在尝试以下查询:
SELECT
c.name,
SUM(CASE
WHEN t.status = 'open' THEN 1
ELSE 0
END),
SUM(CASE
WHEN t.status = 'pending' THEN 1
ELSE 0
END),
SUM(CASE
WHEN t.status = 'closed' THEN 1
ELSE 0
END)
FROM
Ticket t
INNER JOIN
User u
INNER JOIN
UserCoordination uc
INNER JOIN
Coordination c
WHERE
MONTH(t.dateCreated) = :month
GROUP BY 1
但我收到 com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException 并带有导致的消息 您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 1 行的“inner join user_coordinations usercoordi2_ on inner join coordinates coordinat”附近使用正确的语法
感谢您的帮助和时间
【问题讨论】:
标签: mysql sql grails hql grails-orm