代码示例:
in(逗号分隔开的值列表)
释:是否存在于值列表中
---------------------
示例:
select * from test where id in(3,1,5) order by find_in_set(id,'3,1,5');
select * from test where id in(3,1,5) order by substring_index('3,1,2',id,1);
---------------------------
对于in查询并且按照此顺序来显示我们可以使用这种方法:
原语句:
select * from table where id IN (3,6,9,1,2,5,8,7); 这种语句查询出来的顺序还是按照123456789的顺序来的。
可以修改为:
select * from table where id IN (3,6,9,1,2,5,8,7) order by field(id,3,6,9,1,2,5,8,7);
---------------------------
MySQL中NOT IN语句对NULL值的处理
mysql> SELECT COUNT(name) FROM CVE WHERE name NOT IN ('CVE-1999-0001', 'CVE-1999-0002');
+-------------+
| count(name) |
+-------------+
| 17629 |
+-------------+
1 row in set (0.02 sec)
mysql> SELECT COUNT(name) FROM CVE WHERE name NOT IN ('CVE-1999-0001', 'CVE-1999-0002', NULL);
+-------------+
| count(name) |
+-------------+
| 0 |
+-------------+
1 row in set (0.01 sec)
当在子查询中出现NULL的时候,结果就一定是0了。查了一下手册,确实有这样的说法。
所以最后实际采用了这样的查询:
SELECT COUNT(DISTINCT name) FROM CVE WHERE name NOT IN (SELECT cveID FROM cve_sig WHERE cveID IS NOT NULL)