MySQL NOT IN 运算符

MySQL NOT IN operator

我有以下数据,其中包含 2 个不同的 TBL_ID - 337448 和 8612

"TBL_ID","PARAM_KEY","PARAM_VALUE"
 337448,"comment","CHANGE DUE"
 337448,"transient_lastDdlTime","1505760292"
 8612,"COLUMN_STATS_ACCURATE","true"
 8612,"numFiles","1"
 8612,"numRows","5294"
 8612,"rawDataSize","-1"
 8612,"totalSize","113217"
 8612,"transient_lastDdlTime","1452191300"

我需要查询所有不包含"numRows"的TBL_ID 我尝试过的:

SELECT * FROM TABLE_PARAMS 
WHERE PARAM_KEY NOT IN ('numRows')

结果:

"TBL_ID","PARAM_KEY","PARAM_VALUE"
 337448,"comment","CHANGE DUE"
 337448,"transient_lastDdlTime","1505760292"
 8612,"COLUMN_STATS_ACCURATE","true"
 8612,"numFiles","1"
 8612,"rawDataSize","-1"
 8612,"totalSize","113217"
 8612,"transient_lastDdlTime","1452191300"

确实删除了带有 numRows 的列,但我需要删除整个 TBL_ID (8612)

NOT IN ('numRows')

基本不等于

<> 'numRows'

通常,一个人使用一个 NOT IN 和多个值来简化编写一大堆

mycol <> 'a' and mycol <> 'b' and mycol <> 'c'

然后写

mycol NOT IN ('a', 'b', 'c')

或使用像

这样的子查询
mycol NOT IN (SELECT badcols FROM table)

您的用法似乎是 NOT EXISTS 比 NOT IN 更有用的情况。

SELECT * 
FROM TABLE_PARAMS t1
WHERE NOT EXISTS (SELECT * FROM TABLE_PARAMS t2 WHERE t2.tbl_id = t1.tbl_id and t2.param_key<>'numFiles')

或者你可以用像

这样的外连接来做同样的事情
SELECT t1.*
FROM TABLE_PARAMS t1 left outer join TABLE_PARAMS t2
  on t1.tbl_id = t2.tbl_id and t2.param_key='numFiles'
WHERE t2.tbl_id IS NULL

你的问题有点模棱两可。 IN 运算符用于从结果集中查找数据。例如 2 IN {2, 3, 4}。

SELECT TBL_ID FROM  TABLE_PARAMS
WHERE PRAMAR_KEY='numRows'

如果您试图不显示任何与 numRows 关联的 ID,那么您必须首先获取要过滤的 ID,并且用户不等于。 例如,如果您尝试从集合中删除 8612,因为它是 Param_Key ='numRows' 的 ID,并且 如果您尝试在 OPERATOR 中使用, 那么

SELECT * FROM TABLE_PARAMS
WHERE TBL_ID NOT IN (SELECT TBL_ID FROM TABLE_PARAMS WHERE PARAM_KEY='numRows')