0

我想在列中选择数据,但列值不等于''并且 value='NA'。在我的表中,有些列是空的。我想过滤所有非空列并且列有数据值!='NA'。

如何为此要求编写查询。

我在表中有 200 列,因此我无法一一引用列名。column_name 有关键字吗?

4

6 回答 6

1

您可能正在寻找动态查询,尤其是在具有相同条件的查询中涉及许多列的情况下。

SQL Fiddle 上的工作示例

create table t (
  col_1 varchar(10),
  col_2 varchar(10),
  col_3 varchar(10));

insert into t
select  'NA', 'aa', 'aaa' union
select  'b', 'bb', 'NA' union
select  '', 'cc', 'ccc' union
select  'd', 'dd', 'dd' union
select  'e', 'ee', '' union
select  'f', 'ff', 'fff';



SET @q := null;
SELECT @q := concat(coalesce(concat(@q, 'and '), ''), column_name, ' not in (''NA'', '''') ')
FROM information_schema.columns
WHERE table_name='t' and table_schema = DATABASE();
select @q := concat('select * from t where ', @q);

-- select @q;

PREPARE stmt FROM @q;
EXECUTE stmt;

它返回:

COL_1  COL_2  COL_3
d      dd     dd
f      ff     fff
于 2013-06-11T06:12:20.763 回答
1
select * from table where columnName!='' and columnName!='NA'

或者如果你也不需要 NULL

select * from table where columnName!='' and columnName!='NA' and columnName is not null
于 2013-06-11T05:37:21.907 回答
1
SELECT data FROM columns WHERE value = 'NA'

value = 'NA'取代具有空值的列。

于 2013-06-11T05:37:55.890 回答
1
SELECT *
FROM table
WHERE `VALUE` NOT IN ('', 'NA');
于 2013-06-11T05:38:47.557 回答
1

必须这样做:

SELECT * FROM myTable WHERE VALUES <> 'NA' AND VALUES <> ''.
于 2013-06-11T05:39:03.090 回答
0

SELECT * FROM table WHERE 'VALUE' NOT IN ('', 'NA');我不得不插入单个 quato arround VALUE

于 2013-06-12T09:27:19.147 回答