Mysql选择查询使用NOT IN / LIKE来检索数据

时间:2015-10-23 06:44:09

标签: mysql

我需要使用select query查询mysql中的数据,如下所示

Select tid from abc where abc.parameter not like '%DONE%'

但是数据返回参数列的%DONE%VALUE。

我在这里提供样本数据以便更好地理解

tid    parameter    value
***************************
1       abc         123
1       def         456
1       ghi         789

我需要检索def不在的地方。

2 个答案:

答案 0 :(得分:1)

模式

create table abc
(   id int auto_increment primary key,
    tid int not null,
    parameter varchar(20) not null,
    value int not null
);

insert abc(tid,parameter,value) values (1,'abc',123),(1,'def',456),(1,'ghi',789);

查询

select id,tid from abc where abc.parameter not like '%DONE%';
+----+-----+
| id | tid |
+----+-----+
|  1 |   1 |
|  2 |   1 |
|  3 |   1 |
+----+-----+

select id,tid from abc where abc.parameter not like '%abc%';
+----+-----+
| id | tid |
+----+-----+
|  2 |   1 |
|  3 |   1 |
+----+-----+

看起来和我一样。

注意,对于NULL,NOT IN是危险的。一个人必须知道数据,否则使用NOT EXISTS

答案 1 :(得分:0)

你的tid是一样的,返回的数据可能是正确的,但由于你的tid含糊不清,你会觉得结果错了。