[英]How to filter query with empty dummy column in SQL?
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' “ where子句”中的未知列“ fax”
Is it possible? 可能吗?
This is not possible; 这是不可能的; you cannot use aliases defined in the same SELECT
in the WHERE
. 您不能在WHERE
的同一SELECT
中使用定义的别名。
MySQL extends the use of the HAVING
clause, so you can move the conditions to HAVING
: MySQL扩展了HAVING
子句的使用,因此您可以将条件移至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. 符合ANSI SQL的解决方案将适用于大多数dbms产品。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.