自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

MySQL查詢(xún)結(jié)果按某值排序

數(shù)據(jù)庫(kù) MySQL
使用MySQL很多時(shí)候我們不僅只是查詢(xún)出結(jié)果,還需要對(duì)查詢(xún)結(jié)構(gòu)進(jìn)行排序,下文對(duì)查詢(xún)結(jié)果按某值排序的方法作了詳細(xì)的介紹,供您參考。

MySQL查詢(xún)結(jié)果如何排序呢?這是很多人都提過(guò)的問(wèn)題,下面就教您如何對(duì)MySQL查詢(xún)結(jié)果按某值排序,如果您感興趣的話(huà),不妨一看。

之前有一個(gè)功能修改,要求MySQL查詢(xún)結(jié)果中:

id name * * *

1 lucy ...

3 lucy ...

2 lily ...

4 lucy ...

名字為lucy的優(yōu)先排在前面,百思不得其解,可能有人會(huì)說(shuō)簡(jiǎn)單 union嘛 或者弄個(gè)臨時(shí)表什么的,其實(shí)我也想過(guò),但是本身SQL邏輯就很多了(上面只是簡(jiǎn)例),再u(mài)nion的話(huà)或者臨時(shí)表可能繞很大的彎路,后來(lái)看到一篇文章嘗試著加入order by find_in_set(name,'lucy') ,結(jié)果 得到的結(jié)果為lucy全部在下面,隨即我改為order by find_in_set(name,'lucy') desc 實(shí)現(xiàn)結(jié)果為

id name * * *

1 lucy ...

3 lucy ...

4 lucy ...

2 lily ...

基本實(shí)現(xiàn),可是又有點(diǎn)不確定的心情,查mysql文檔發(fā)現(xiàn)find_in_set語(yǔ)法

  1. FIND_IN_SET(str,strlist)   
  2.  

假如字符串str 在由N 子鏈組成的字符串列數(shù)據(jù)表strlist 中, 則返回值的范圍在 1 到 N 之間 。一個(gè)字符串列數(shù)據(jù)表就是一個(gè)由一些被『,』符號(hào)分開(kāi)的自鏈組成的字符串。如果***個(gè)參數(shù)是一個(gè)常數(shù)字符串,而第二個(gè)是type SET列,則   FIND_IN_SET() 函數(shù)被優(yōu)化,使用比特計(jì)算。如果str不在strlist 或strlist 為空字符串,則返回值為 0 。如任意一個(gè)參數(shù)為NULL,則返回值為 NULL。 這個(gè)函數(shù)在***個(gè)參數(shù)包含一個(gè)逗號(hào)(『,』)時(shí)將無(wú)法正常運(yùn)行

  1. mysql> SELECT FIND_IN_SET('b','a,b,c,d');  
  2.  
  3.         -> 2  
  4.  

看了這個(gè)我估計(jì)結(jié)果為什么要加desc 了 find_in_set返回的值是,當(dāng)存在lucy的時(shí)候 返回他的位置,沒(méi)有的時(shí)候?yàn)?,空的時(shí)候null,所以排序?yàn)?,1,1,0,如果加在列上就為

id name FIND_IN_SET * *

1 lucy 1 ...

3 lucy 1 ...

2 lily 0 ...

4 lucy 1...

表結(jié)構(gòu)如下:

  1. mysql> select * from test;  
  2. +----+-------+  
  3. | id | name   |  
  4. +----+-------+  
  5. |   1 | test1 |  
  6. |   2 | test2 |  
  7. |   3 | test3 |  
  8. |   4 | test4 |  
  9. |   5 | test5 |  
  10. +----+-------+  

執(zhí)行以下SQL:

  1. mysql> select * from test where id in(3,1,5);  
  2. +----+-------+  
  3. | id | name   |  
  4. +----+-------+  
  5. |   1 | test1 |  
  6. |   3 | test3 |  
  7. |   5 | test5 |  
  8. +----+-------+  
  9. 3 rows in set (0.00 sec)  

這個(gè)select在mysql中得結(jié)果會(huì)自動(dòng)按照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

方法如下:

  1. select * from test where id in(3,1,5) order by find_in_set(id,'3,1,5');  
  2. select * from test where id in(3,1,5) order by substring_index('3,1,2',id,1);  

兩者均可

 

 

 

【編輯推薦】

使用函數(shù)實(shí)現(xiàn)MySQL查詢(xún)行號(hào)

MySQL查詢(xún)中的非空問(wèn)題

MySQL日期函數(shù)和時(shí)間函數(shù)

兩種常用MySql查詢(xún)時(shí)間段的方法

深度解析MySQL查詢(xún)緩存機(jī)制

責(zé)任編輯:段燃 來(lái)源: 互聯(lián)網(wǎng)
相關(guān)推薦

2010-07-16 09:00:08

SQL Server查

2010-11-25 14:45:19

MySQL查詢(xún)結(jié)果

2015-09-08 10:16:41

Java參數(shù)按值傳遞

2010-11-25 15:30:15

MySQL查詢(xún)結(jié)果

2010-11-25 15:36:09

MySQL查詢(xún)結(jié)果集

2011-04-06 10:53:36

MySQL

2009-09-14 10:09:26

LINQ查詢(xún)結(jié)果

2021-09-02 22:52:16

ValueDictionary排序

2009-06-18 13:58:06

Hibernate多表Hibernate

2010-10-15 11:05:31

MYSQL查詢(xún)結(jié)果

2010-11-24 11:52:15

MYSQL表字段最大值

2024-03-15 09:50:00

NULLSQL優(yōu)化

2009-08-21 17:53:28

C#查詢(xún)結(jié)果

2010-11-25 14:49:08

MySQL查詢(xún)最大值

2022-06-21 08:13:34

MySQL查詢(xún)數(shù)據(jù)庫(kù)

2023-05-12 17:45:15

MySQL索引排序

2011-07-13 13:39:46

CHARINDEXSqlServer

2024-05-22 09:01:53

InnoDBB+索引

2020-12-31 08:05:27

MySQL服務(wù)器版本號(hào)

2011-10-11 10:49:25

Oracle
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)