SQL 自訂排序

來源:互聯網
上載者:User

--用case排序 CASE c_id  WHEN '15' THEN 1 else 2 end
select top 4 c.* from m3_channel c where  isnull(c.c_upload_mediaserver,'-1') <> '0'
ORDER BY CASE c_id  WHEN '15' THEN 1 WHEN '14' THEN 2 WHEN '17' THEN 3 WHEN '16' THEN 4 ELSE 5  END

--用charindex排序 所有的都指定
select  * from m3_channel  where  isnull(c_upload_mediaserver,'-1') <> '0'
order by charindex(','+cast(c_id as varchar(20))+',',',14,16,13,15,18,17,')
--用charindex排序 c_id in 的ID都指定
select top 4 * from m3_channel  where  isnull(c_upload_mediaserver,'-1') <> '0' and c_id in (16,15,14,13)
order by charindex(','+cast(c_id as varchar(20))+',',',14,16,13,15,')

--SQL變數
DECLARE @SortKey VARCHAR(300)
SET @SortKey='直播頻道4,直播頻道3,直播頻道6,直播頻道5'
select top 4 c.* from m3_channel c where  isnull(c.c_upload_mediaserver,'-1') <> '0'
ORDER BY CHARINDEX(c_name,@SortKey)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.