3

ステートメントと等しくない値自体を選択する必要があります。

何かのようなもの

SELECT * FROM table WHERE * != "qwerty"

しかし、好きではない

SELECT * FROM table WHERE column_name != "qwerty"

どうやってやるの?

私は次のようなテーブルを持っています

       1   2   3   4   5   6   7   8   9   10   11   ...   ...
    1  a   b   c   d   t   h   v   h   d   t    y    ...   ...
    2  g   t   5   s   h   r   q   q   q   q    q    ...   ...
   ... ...
   ... ...

「q」と等しくないすべての値を選択する必要があります

私はsmthのようにすることができます

SELECT * WHERE 1 != q AND 2 != q AND 3 != q ...

しかし、列が多すぎます

4

3 に答える 3

7

これを試して:

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

于 2012-05-06T07:42:24.420 に答える
0

多分あなたはSHOW COLUMNSを試すことができます:

SHOW COLUMNS FROM SomeTable

これにより、すべての列情報が返されます。

例:

    [Field] => id
    [Type] => int(7)
    [Null] =>  
    [Key] => PRI
    [Default] =>
    [Extra] => auto_increment

その後、Michael Buenの回答を使用して、必要な値を取得できます。

SELECT * FROM table WHERE "qwerty" NOT IN (columnName1,columnName2,columnName3,columnName4,etc)
于 2012-05-06T08:08:13.333 に答える