2012-01-30 98 views
9

我試圖運行此查詢按升序排列:MySQL的多列遞增順序

SELECT title,project_index 
FROM projectdetail 
WHERE project_index BETWEEN 1 AND 6 
ORDER BY title, project_index ASC; 

我需要按升序排列兩列,但上面的查詢返回結果與ASC以便只有一列。

+8

我不完全搞定你的問題。但你可以指定每個列的順序,比如'order by col1 asc,col2 asc,col3 desc'等等 – 2012-01-30 11:32:17

+0

我試過但它不適用於我..我的查詢是title,project_index這兩列我想顯示asc但只有一列生效。 – user1135693 2012-01-30 11:34:24

+0

什麼是您的數據庫? – 2012-01-30 11:35:17

回答

3

如果您使用的是mysql,請檢查this

正如他們說有,你可以使用SELECT * FROM t1 ORDER BY key_part1 DESC, key_part2 ASC;

+0

感謝您的答覆.....但仍然我面臨這個問題,我嘗試以上所有,但仍然....... – user1135693 2012-01-30 11:46:37

24

升序是默認對於大多數(如果不是全部) DBMS的所以你的說法是種在這方面怪異但儘管如此,你可以通過向其添加說明符ASCDESC來爲每個單獨列指定一個訂單。然後

你的聲明將成爲

SELECT title 
     , project_index 
FROM projectdetail 
WHERE project_index BETWEEN 1 AND 6 
ORDER BY 
     title ASC 
     , project_index ASC 

編輯

正如@Arvo & @Dems被提及,目前正在排序第一title以及有關project_index相同的標題。如果您希望project_index先排序,則必須先將其置於ORDER BY子句中。

你的說法就變成

SELECT title 
     , project_index 
FROM projectdetail 
WHERE project_index BETWEEN 1 AND 6 
ORDER BY 
     project_index ASC 
     , title ASC 

因爲ASC是默認的排序順序,則可以省略這些產品總數

SELECT title 
     , project_index 
FROM projectdetail 
WHERE project_index BETWEEN 1 AND 6 
ORDER BY 
     project_index 
     , title 
+0

我的數據庫mysql – user1135693 2012-01-30 11:37:07

+0

我試過上面的查詢,但project_index沒有采取它顯示desc的效果... 。 – user1135693 2012-01-30 11:38:26

+0

@ user1135693 - 你能發佈你的查詢和結果嗎?查詢出現問題,或者您偶然發現錯誤。 – 2012-01-30 11:39:43

0

您嘗試按升序排列兩列進行排序。在MySQL中,您可以在查詢中使用多個順序。但是在這裏,訂單的偏好非常重要。第一個獲得最多的偏好,第二個獲得第二個偏好。
這意味着,您所查詢的是

SELECT title,project_index FROM projectdetail 
    WHERE project_index BETWEEN 1 AND 6 ORDER BY title, project_index ASC; 

其中,爲了用標題了第一選擇。 mysql將首先按升序排列「標題」列並顯示結果。那麼只有它會命令'project_index'列。所以你無法得到你想要的答案。

0

試試這個:

SELECT title, project_index 
FROM projectdetail 
WHERE project_index BETWEEN 1 AND 6 
ORDER BY project_index, title; 
-1

你可以用下面的嘗試和check--

SELECT title,project_index 
FROM projectdetail 
WHERE project_index BETWEEN 1 AND 6 
ORDER BY title, project_index 
-1

根據您的要求/查詢我認爲這是不可能做到的訂貨超過2列在同一張桌子裏。如果你想根據價值進行訂購,你可以這樣做。

SELECT lat,lon, title, zip, city, state, region,cantone 
FROM company 
WHERE title != '' AND state IN(1,3,4,5,6,7,9,2) 
ORDER BY state=2,title asc 

在上面的查詢中,它將首先顯示除state = 2之外的所有標題,然後顯示state = 2的所有記錄。

3

ORDER BY標題ASC,project_index ASC;

SELECT title,project_index 
FROM projectdetail 
WHERE project_index BETWEEN 1 AND 6 
ORDER BY title ASC, project_index ASC; 

,你可以添加喜歡ORDER BY col1 ASC, col2 ASC, col3 DESC;

-2

使用

ORDER BY title ASC,project_index ASC 

,而不是

ORDER BY title, project_index ASC; 

多個列,分離得到的訂單都那麼它會正常工作。

-2

你可以試試:

SELECT title, project_index FROM projectdetail 
ORDER BY title ASC, project_index DESC