[英]Select a dummy column with a dummy value and search in dummy column in SQL?
[英]How to filter query with empty dummy column in SQL?
我有情况必须 像比较空的空场。
SELECT id, name, contact, landline, '' fax
FROM table1
WHERE contact LIKE '%123%'
OR landline LIKE '%123%'
OR fax LIKE '%123%'
执行后,它给出如下错误:
“ where子句”中的未知列“ fax”
可能吗?
这是不可能的; 您不能在WHERE
的同一SELECT
中使用定义的别名。
MySQL扩展了HAVING
子句的使用,因此您可以将条件移至HAVING
:
SELECT id, name, contact, landline, '' as fax
FROM table1
HAVING contact LIKE '%123%' OR landline LIKE '%123%' OR fax LIKE '%123%'
将原始查询包装在派生表中(子查询)。 将条件应用于其结果:
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的解决方案将适用于大多数dbms产品。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.