Mysql select in 按id排序?qū)崿F(xiàn)方法
更新時間:2013年03月17日 14:55:31 作者:
有時候我們在后臺選擇了一系列的id,我們想安裝填寫id的順序進(jìn)行排序,那么就需要下面的order by方法,測試通過
表結(jié)構(gòu)如下:
mysql> select * from test;
+----+-------+
| id | name |
+----+-------+
| 1 | test1 |
| 2 | test2 |
| 3 | test3 |
| 4 | test4 |
| 5 | test5 |
+----+-------+
執(zhí)行以下SQL:
mysql> select * from test where id in(3,1,5);
+----+-------+
| id | name |
+----+-------+
| 1 | test1 |
| 3 | test3 |
| 5 | test5 |
+----+-------+
3 rows in set (0.00 sec)
這個select在mysql中得結(jié)果會自動按照id升序排列,
但是我想執(zhí)行"select * from test where id in(3,1,5);"的結(jié)果按照in中得條件排序,即:3,1,5,
想得到的結(jié)果如下:
id name
3 test3
1 test1
5 test5
請問在這樣的SQL在Mysql中怎么寫?
網(wǎng)上查到sqlserver中可以用order by charindex解決,但是沒看到Mysql怎么解決??請高手幫忙,謝
謝!
select * from a order by substring_index('3,1,2',id,1);
試下這個good,ls正解。
order by find_in_set(id,'3,1,5')
謝謝,經(jīng)測試order by substring_index和order by find_in_set都可以
mysql> select * from test;
+----+-------+
| id | name |
+----+-------+
| 1 | test1 |
| 2 | test2 |
| 3 | test3 |
| 4 | test4 |
| 5 | test5 |
+----+-------+
執(zhí)行以下SQL:
mysql> select * from test where id in(3,1,5);
+----+-------+
| id | name |
+----+-------+
| 1 | test1 |
| 3 | test3 |
| 5 | test5 |
+----+-------+
3 rows in set (0.00 sec)
這個select在mysql中得結(jié)果會自動按照id升序排列,
但是我想執(zhí)行"select * from test where id in(3,1,5);"的結(jié)果按照in中得條件排序,即:3,1,5,
想得到的結(jié)果如下:
id name
3 test3
1 test1
5 test5
請問在這樣的SQL在Mysql中怎么寫?
網(wǎng)上查到sqlserver中可以用order by charindex解決,但是沒看到Mysql怎么解決??請高手幫忙,謝
謝!
select * from a order by substring_index('3,1,2',id,1);
試下這個good,ls正解。
order by find_in_set(id,'3,1,5')
謝謝,經(jīng)測試order by substring_index和order by find_in_set都可以
您可能感興趣的文章:
- Mysql中的排序規(guī)則utf8_unicode_ci、utf8_general_ci的區(qū)別總結(jié)
- 讓MySQL支持中文排序的實(shí)現(xiàn)方法
- mysql如何根據(jù)漢字首字母排序
- Mysql row number()排序函數(shù)的用法和注意
- 兩種mysql對自增id重新從1排序的方法
- 數(shù)據(jù)庫查詢排序使用隨機(jī)排序結(jié)果示例(Oracle/MySQL/MS SQL Server)
- MySQL中按照多字段排序及問題解決
- MySQL查詢優(yōu)化:連接查詢排序limit(join、order by、limit語句)介紹
- Mysql利用group by分組排序
- MySQL中一些鮮為人知的排序方式
相關(guān)文章
Mysql中in和exists的區(qū)別?&?not?in、not?exists、left?join的相互轉(zhuǎn)換問題
這篇文章主要介紹了Mysql中in和exists的區(qū)別?&?not?in、not?exists、left?join的相互轉(zhuǎn)換,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-09-09Navicat遠(yuǎn)程連接SQL Server并轉(zhuǎn)換成MySQL步驟詳解
最近遇到一個需求是將SQL Server轉(zhuǎn)換為 MySQL的格式,由于不想在本地安裝 SQL Server,所以決定在遠(yuǎn)程的 Windows 服務(wù)器上安裝,并在本地使用Navicat遠(yuǎn)程連接它,然而在實(shí)現(xiàn)過程中遇到了諸多問題,記錄于此。感興趣的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧。2017-01-01mysql server is running with the --skip-grant-tables option
今天在mysql中新建數(shù)據(jù)庫提示The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement,原來是數(shù)據(jù)中配置的--skip-grant-tables,這樣安全就降低了,這個一般當(dāng)忘記root密碼的時候需要這樣操作2017-07-07mysql自動備份多個數(shù)據(jù)庫的實(shí)現(xiàn)
本文主要介紹了mysql自動備份多個數(shù)據(jù)庫的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07MySQL命令行導(dǎo)出導(dǎo)入數(shù)據(jù)庫實(shí)例詳解
這篇文章主要介紹了MySQL命令行導(dǎo)出導(dǎo)入數(shù)據(jù)庫實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2016-10-10MySQL數(shù)據(jù)庫Event定時執(zhí)行任務(wù)詳解
這篇文章主要介紹了MySQL數(shù)據(jù)庫Event定時執(zhí)行任務(wù)2017-12-12