我有以下数据,包含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)
答案 0 :(得分:2)
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
答案 1 :(得分:2)
你的问题有点含糊不清。 IN运算符用于从结果集中查找数据。例如,2 IN {2,3,4}。
SELECT TBL_ID FROM TABLE_PARAMS
WHERE PRAMAR_KEY='numRows'
如果您尝试不显示与numRows关联的任何ID,则必须首先获取要过滤的ID,并将用户NOT EQUALS设置为。 例如,如果您尝试从集合中删除8612,因为它是Param_Key ='numRows'和的ID,如果您正在尝试在操作员中使用,那么
SELECT * FROM TABLE_PARAMS
WHERE TBL_ID NOT IN (SELECT TBL_ID FROM TABLE_PARAMS WHERE PARAM_KEY='numRows')