MySQL 关于 COALESCE 语法的条件
我有一个由 COALESCE 语法组成的查询。 COALESCE 语法只是查找文件的审核日期。在我的 WHERE 语句中,我只想从 AuditDate
别名字段输出审核文件库,但我不起作用。这是我的查询。
SELECT COALESCE(qua.starttime, prd.starttime) AS `AuditDate`,
prd.employeeno AS `EmployeeNo`,
prd.starttime AS `StartTime`,
prd.endtime AS `EndTime`
FROM production prd
LEFT JOIN qualityaudit qua
ON prd.id=qua.id
WHERE `AuditDate` BETWEEN '2011-10-01 00:00:00' AND '2011-10-01 23:59:59';
任何人都可以帮助我如何正确地获得此查询?
I have a query composing of COALESCE syntax. the COALESCE syntax simply find the Audited Date of a file. On my WHERE statement, I just want to output the Audited File base from the AuditDate
alias field but i doesn't work. Here is my query.
SELECT COALESCE(qua.starttime, prd.starttime) AS `AuditDate`,
prd.employeeno AS `EmployeeNo`,
prd.starttime AS `StartTime`,
prd.endtime AS `EndTime`
FROM production prd
LEFT JOIN qualityaudit qua
ON prd.id=qua.id
WHERE `AuditDate` BETWEEN '2011-10-01 00:00:00' AND '2011-10-01 23:59:59';
Can anyone help me on how should I get this query correct?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您的查询需要像这样适合
您的情况,您不能使用来自函数结果的别名作为
WHERE
子句中的查询条件(请参阅 http://dev.mysql.com/doc/refman/5.0/en/problems-with-alias .html)。但是您可以使用别名在HAVING
子句中进行搜索,因为 MySQL 5.0.2 及更高版本允许HAVING
子句引用 SELECT 中 select_expr 中指定的任何列或别名列表或外部子查询,以及聚合函数,就像两者都有相同的结果
Your query need to be like this
for your situation you can't use the alias that came from a result of a function as query criteria in
WHERE
clause (see http://dev.mysql.com/doc/refman/5.0/en/problems-with-alias.html). But you can use the alias to search inHAVING
clause, as MySQL 5.0.2 and up permitHAVING
clause to refer to any column or alias named in a select_expr in the SELECT list or in outer subqueries, and to aggregate functions, likeboth have same result
使用
上面假设您的日期格式/列类型确实匹配...
use
The above assumes that your date format/column types really match...
您不能像其他人指出的那样使用别名。为了避免调用合并两次,您可以使用 HAVING 语句。
You cannot use an alias as others have pointed out. To avoid calling coalesce two times, you can use a HAVING statement.
您不能在
WHERE
子句中使用SELECT
列表中的别名。使用这个:
或复制代码:
You can't use an alias from the
SELECT
list in theWHERE
clause.Use this:
or duplicate the code: