MySQL查詢in操作 查詢結果按in集合順序顯示

2021-07-09 20:21:15 字數 3004 閱讀 7174

引自:

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]-[0-9]$' 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)這兩條記錄,即找出其它記錄

error: select * from info where episode_id=100034 and id not in(3,4) and info_type_id not in (8);

error result:

id, info_type_id, programme_id, episode_id

100001, 4, 382, 100034

100002, 4, 382, 100034

correct: select * from info where episode_id=100034 and (id<>3 or info_type_id<>8) and (id<>4 or info_type_id<>8);

correct result:

id, info_type_id, programme_id, episode_id

3, 4, 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 or info_type_id<>8排除掉id=3 && info_type_id=8這條記錄,當表中主鍵多於乙個時,不能簡單地使用key1 not in (……) and key2 not in (……) ..

c 操作mysql 查詢結果集

int mysql query mysql mysql,const char query 查詢函式,成功返回零,否則返回錯誤型別非零值。const char mysql error mysql mysql 返回最近一次呼叫失敗的錯誤訊息 返回的是字串訊息 unsigned int mysql err...

Mysql查詢結果亂碼

檢視mysql資料庫伺服器和資料庫字符集 檢視表中所有列的字符集 檢視表的字符集 檢視mysql所支援的字符集 修改全域性字符集 set character set connection utf8 set character set database utf8 set character set r...

MYSQL 動態查詢結果

今天遇到乙個需求需要把這樣的 轉化為這樣的 第一張圖是從臨時表來的,也就是第二個圖的列名是不固定的,如果列名是固定的就是乙個簡單的行轉列,但是現在列名不固定,我們必須使用動態的行轉列,完成這個裝換我們需要使用兩個知識點 1.使用動態語句執行sql set sql concat create temp...