按指定排列順序獲取數(shù)據(jù)的sql語句_Mssql數(shù)據(jù)庫教程
推薦:總結(jié)經(jīng)典常用的SQL語句(2)向表中添加一個(gè)新記錄,你要使用SQLINSERT語句。 這里有一個(gè)如何使用這種語句的例子: INSERTmytable(mycolumn)VALUES(‘somedata’) 這個(gè)語句把字符串’somedata’插入表mytable的mycolumn字段中。將要被插入數(shù)據(jù)的字段的名字在第一個(gè)括號中指定,實(shí)際的數(shù)
測試tablecreate table table1 (id int,name char)
insert into table1
select 1,'q'
union all select 2,'r'
union all select 3,'3'
union all select 4,'5'
要求按指定的id順序(比如2,1,4,3)排列獲取table1的數(shù)據(jù)
方法1:使用union all,但是有256條數(shù)據(jù)的限制
select id,name from table1 where id=2
union all
select id,name from table1 where id=1
union all
select id,name from table1 where id=4
union all
select id,name from table1 where id=3
方法2:在order by中使用case when
select id ,name from t where id in (2,1,4,3)
order by (case id
when 2 then 'A'
when 1 then 'B'
when 4 then 'C'
when 3 then 'D' end)
*以上兩種方法適合在數(shù)據(jù)量非常小的情況下使用
方法3:使用游標(biāo)和臨時(shí)表
先建一個(gè)輔助表,里面你需要的順序插入,比如2,1,4,3
create table t1(id int)
insert into t1
select 2
union all select 1
union all select 4
union all select 3
declare @id int --定義游標(biāo)
declare c_test cursor for
select id from t1
select * into #tmp from table1 where 1=2 --構(gòu)造臨時(shí)表的結(jié)構(gòu)
OPEN c_test
FETCH NEXT FROM c_test
INTO @id
WHILE @@FETCH_STATUS = 0
BEGIN
--按t1中的id順序插數(shù)據(jù)到臨時(shí)表
insert into #tmp select id,name from table1 where id=@id
FETCH NEXT FROM c_test INTO @id
End
Close c_test
deallocate c_test
*該方法適合需要按照輔助表的順序重排table的順序時(shí)使用
(即輔助表已經(jīng)存在的情況)
分享:總結(jié)經(jīng)典常用的SQL語句(1)說明:復(fù)制表(只復(fù)制結(jié)構(gòu),源表名:a新表名:b) SQL:select*intobfromawhere11 說明:拷貝表(拷貝數(shù)據(jù),源表名:a目標(biāo)表名:b) SQL:insertintob(a,b,c)selectd,e,ffromb; 說明:顯示文章、提交人和最后回復(fù)時(shí)間 SQL:selecta.title,a.username,b.adddatefromtab
- sql 語句練習(xí)與答案
- 深入C++ string.find()函數(shù)的用法總結(jié)
- SQL Server中刪除重復(fù)數(shù)據(jù)的幾個(gè)方法
- sql刪除重復(fù)數(shù)據(jù)的詳細(xì)方法
- SQL SERVER 2000安裝教程圖文詳解
- 使用sql server management studio 2008 無法查看數(shù)據(jù)庫,提示 無法為該請求檢索數(shù)據(jù) 錯(cuò)誤916解決方法
- SQLServer日志清空語句(sql2000,sql2005,sql2008)
- Sql Server 2008完全卸載方法(其他版本類似)
- sql server 2008 不允許保存更改,您所做的更改要求刪除并重新創(chuàng)建以下表
- SQL Server 2008 清空刪除日志文件(瞬間日志變幾M)
- Win7系統(tǒng)安裝MySQL5.5.21圖解教程
- 將DataTable作為存儲過程參數(shù)的用法實(shí)例詳解
Mssql數(shù)據(jù)庫教程Rss訂閱編程教程搜索
Mssql數(shù)據(jù)庫教程推薦
- 在SQL Server實(shí)例之間傳輸?shù)卿浐兔艽a
- 無法在com+ 目錄中安裝和配置程序集 錯(cuò)誤:-2146233087的解決方法[已測]
- MyEclipse 配置SQL Server 2008數(shù)據(jù)庫驅(qū)動(dòng)操作步驟
- 多種還原.bak數(shù)據(jù)庫文件方式
- Mssql數(shù)據(jù)庫教程之--SQL Server 2008:傳遞表值參數(shù)
- 如何解決MySQL 5數(shù)據(jù)庫連接超時(shí)問題
- 如何提高SQL語句性能
- 淺談SQL Server 2008數(shù)據(jù)庫中的新數(shù)據(jù)類型
- 解析J2EE中SQL語句自動(dòng)構(gòu)造方法
- 如何使用SQL Server數(shù)據(jù)庫中查詢累計(jì)值
猜你也喜歡看這些
- jdbc調(diào)用mysql存儲過程實(shí)現(xiàn)代碼
- MySQL數(shù)據(jù)庫INSERT、UPDATE、DELETE以及REPLACE語句的用法詳解
- MySQL筆記之別名的使用
- MySQL筆記之函數(shù)查詢的使用
- 基于MYSQL中優(yōu)化的一些方法
- 網(wǎng)站數(shù)據(jù)多了分頁慢該怎么辦?
- 如何用命令行進(jìn)入mysql具體操作步驟
- 深入mysql "ON DUPLICATE KEY UPDATE" 語法的分析
- MySQL中優(yōu)化sql語句查詢常用的30種方法
- Mysql中文亂碼以及導(dǎo)出為sql語句和Excel問題解決方法[圖文]
- 相關(guān)鏈接:
- 教程說明:
Mssql數(shù)據(jù)庫教程-按指定排列順序獲取數(shù)據(jù)的sql語句。