在条件Mysql中使用时,别名变量不起作用

时间:2014-07-15 16:51:37

标签: php mysql

在条件client != id

中使用时,别名变量无效
select *,str_to_date(invoice_date,'%d/%m/%Y') as date,invoice_to as id, 
    (select mob_no from client_info where mob_no = id ) as client
from client_invoice
where client != id
having str_to_date(invoice_date,'%d/%m/%Y') >= '$fdate'
   and str_to_date(invoice_date,'%d/%m/%Y') <= '$tdate'  
ORDER BY date DESC

请帮助解决此问题。在此先感谢。

1 个答案:

答案 0 :(得分:2)

您无法在WHERE子句中引用别名,您必须使用HAVING

SELECT *, str_to_date(invoice_date, '%d/%m/%Y') AS date,
       invoice_to AS id,
       (SELECT mob_no FROM client_info WHERE mob_no = invoice_to) AS client
FROM client_invoice
WHERE STR_TO_DATE(invoice_date, '%d/%m/%Y') BETWEEN '$fdate' AND '$tdate'
HAVING client != id