我有一個表table1
(id, col1, col2, col3, text
),id
是主鍵,並且它有一個聚集索引。SQL Server如何命令集羣索引表上的SELECT語句的結果集
當我運行查詢
Select * from table1
我得到的結果如下,這是預期
id col1 col2 col3 text
---------------------------------
779 027 15 001 test1
780 027 15 600 test1
781 027 15 001 test2
782 027 15 600 test2
783 027 15 001 test3
784 027 15 600 test3
,但如果我運行下面的查詢
select * from table1
order by col1, col2, col3
我得到這個
id col1 col2 col3 text
---------------------------------
779 027 15 001 test1
781 027 15 001 test2
783 027 15 001 test3
784 027 15 600 test3
782 027 15 600 test2
780 027 15 600 test1
我的問題是爲什麼最後三條記錄與前三條記錄的順序相反?
你想用'COL1,COL2命令行, col3' - 你明白了 - 對吧?這沒什麼錯 - 你得到你要求的。任何其他(沒有明確的ORDER BY'的任何進一步的「排序」)都不能保證--SQL Server可以以任何順序返回數據(以及任何順序最快的) –