溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

SQL查詢按指定字段排序

發(fā)布時(shí)間:2020-07-25 08:28:14 來源:網(wǎng)絡(luò) 閱讀:527 作者:羅建春 欄目:關(guān)系型數(shù)據(jù)庫
city_name是字段名,現(xiàn)在要按("北京","上海","廣州")   先后順序把數(shù)據(jù)查詢統(tǒng)計(jì)出來?

不知道你什么數(shù)據(jù)庫, 假如是 Oracle:

select   
  count(city_name),
  city_name 
from   
  tuan_info   
where 
  source_type  = 1 
  and  city_name in("北京","上海","廣州")   
group    by 
  city_name
ORDER BY
  INSTR('北京,上海,廣州', city_name);

如果是 SQL Server 用
ORDER BY CHARINDEX(city_name, '北京,上海,廣州')

追問

是SQL Server 的,怎么寫哦,給個(gè)完整點(diǎn)的語句好嗎

回答

select   
  count(city_name),
  city_name 
from   
  tuan_info   
where 
  source_type  = 1 
  and  city_name in("北京","上海","廣州")   
group    by 
  city_name
ORDER BY CHARINDEX(city_name, '北京,上海,廣州')

前面的SQL都是一樣的啊。
只是 增加一個(gè)  ORDER BY 的排序處理。

追問

在嗎,不行的,提示有錯(cuò)啊,是什么問題哦
[SQL] select count(city_name), city_name from tuan_info where source_type  = 1 and  city_name in("北京","上海","廣州") group by city_name ORDER BY CHARINDEX(city_name, '北京,上海,廣州');
[Err] 1305 - FUNCTION data_group.CHARINDEX does not exist

回答

你這個(gè)是什么數(shù)據(jù)庫?。?
CHARINDEX  是 SQL Server 自帶的函數(shù)啊。

追問

那個(gè),,是MYSQL的哦,怎辦

回答

MySQL  就換一個(gè)函數(shù)   INSTR

select   
  count(city_name),
  city_name 
from   
  tuan_info   
where 
  source_type  = 1 
  and  city_name in("北京","上海","廣州")   
group    by 
  city_name
ORDER BY INSTR( '北京,上海,廣州',   city_name)



向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI