尝试这个:
SELECT * FROM table WHERE "qwerty" NOT IN (column1,column2,column3,column4,etc)
另一个例子:
-- this...
SELECT 'HELLO!' FROM tblx
WHERE 'JOHN' NOT IN (col1,col2,col3);
-- ...is semantically equivalent to:
SELECT 'HELLO!' FROM tblx
WHERE 'JOHN' <> col1
AND 'JOHN' <> col2
AND 'JOHN' <> col3;
数据源:
create table tblx(col1 text,col2 text,col3 text);
insert into tblx values
('GEORGE','PAUL','RINGO'),
('GEORGE','JOHN','RINGO');
如果您使用的是 Postgresql,则可以为列创建快捷方式:
select *
from
(
select
row(tblx.*)::text AS colsAsText,
translate(row(tblx.*)::text,'()','{}')::text[]
as colsAsArray
from tblx
) x
where 'JOHN' <> ALL(colsAsArray)
现场测试:http ://www.sqlfiddle.com/#!1/8de35/2
Postgres 可以从数组中生成行,'JOHN' <> ALL
相当于::
where 'JOHN' NOT IN (SELECT unnest(colsAsArray))
现场测试:http ://www.sqlfiddle.com/#!1/8de35/6
如果上面真的是你想要实现的,如果你使用全文搜索,搜索会好很多
对于 MySQL:
select
@columns := group_concat(column_name)
from information_schema.columns
where table_name = 'tblx'
group by table_name;
set @dynStmt :=
concat('select * from tblx where ? NOT IN (', @columns ,')');
select @dynStmt;
prepare stmt from @dynStmt;
set @filter := 'JOHN';
execute stmt using @filter;
deallocate prepare stmt;
现场测试:http ://www.sqlfiddle.com/#!2/8de35/49