>데이터 베이스 >MySQL 튜토리얼 >MySQL查询in操作 查询结果按in集合顺序显示_MySQL

MySQL查询in操作 查询结果按in集合顺序显示_MySQL

WBOY
WBOY원래의
2016-06-01 13:20:361014검색

bitsCN.com MySQL 查询in操作,查询结果按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);


偶尔看到的。。。或许有人会注意过,但我以前真不知道
SQL: select * from table where id IN (3,6,9,1,2,5,8,7);

这样的情况取出来后,其实,id还是按1,2,3,4,5,6,7,8,9,排序的,但如果我们真要按IN里面的顺序排序怎么办?SQL能不能完成?是否需要取回来后再foreach一下?其实mysql就有这个方法

sql: 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)
顺便提一下MySQL中正则表达式匹配的简单使用:
SELECT COUNT(alarmID)
FROM Alarm
WHERE (CVE NOT RLIKE '^CVE-[0-9]{4}-[0-9]{4}$' OR CVE IS NULL)
当然,RLIKE也可以写作REGEXP,我个人倾向于使用RLIKE,因为拼写接近LIKE,可以见名知义。

mysql - not in
table:info primary key(id, info_type_id)
id, info_type_id, programme_id, episode_id
3, 4, 382, 100034
3, 8, 382, 100034
4, 8, 382, 100034
6, 8, 382, 100034
7, 8, 382, 100034
8, 8, 382, 100034
9, 8, 382, 100034
10, 8, 382, 100034
11, 8, 382, 100034
12, 8, 382, 100034
13, 8, 382, 100034
100001, 4, 382, 100034
100002, 4, 382, 100034

排除(id=3 && info_type_id=8) and (id=4 && info_type_id=8)

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.