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?
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
WHEREclause (see http://dev.mysql.com/doc/refman/5.0/en/problems-with-alias.html). But you can use the alias to search inHAVINGclause, as MySQL 5.0.2 and up permitHAVINGclause 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