I have situation where must compare empty dummy field in like .
SELECT id, name, contact, landline, '' fax
FROM table1
WHERE contact LIKE '%123%'
OR landline LIKE '%123%'
OR fax LIKE '%123%'
After execution it gives error like:
Unknown column 'fax' in 'where clause'
Is it possible?
This is not possible; you cannot use aliases defined in the same SELECT
in the WHERE
.
MySQL extends the use of the HAVING
clause, so you can move the conditions to HAVING
:
SELECT id, name, contact, landline, '' as fax
FROM table1
HAVING contact LIKE '%123%' OR landline LIKE '%123%' OR fax LIKE '%123%'
Wrap the original query up in a derived table (sub-query). Apply the conditions on its result:
select id, name, contact, landline, fax
from
(
SELECT id, name, contact, landline, '' fax
FROM table1
) dt
WHERE contact LIKE '%123%'
OR landline LIKE '%123%'
OR fax LIKE '%123%'
ANSI SQL compliant solution, will work with most dbms products.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.