Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

Yuta
Lv 7
Yuta asked in 電腦與網際網路程式設計 · 1 decade ago

MYSQL 兩個以上 order by 條件設定

一個很簡單不過我基礎不好的問題 = = ...

就是我的欄位有三欄

我想依序 a b c 來做排序,先排 a 如果都一樣 再排 b 再一樣再對 c

(譬如年月日好了)

我下的指令是 order by `year` DESC,`month` DESC,`day` DESC 當然錯了 ... @@ ...

因為上網有找到是

ORDER BY field1 , field2 ...

所以以為是加 , 就好

有其他寫法嗎 ?? 寫兩個好像可以,三個就錯了 ??

謝謝

2 Answers

Rating
  • 爵士
    Lv 5
    1 decade ago
    Favorite Answer

    MYSQL ORDER 指令不能有 ` 這個符號

    這一行是錯的 order by `year` DESC,`month` DESC,`day` DESC

    試試看

    SELECT * FROM yourTable order by year DESC,month DESC,day DESC

    底下這行是一樣的

    SELECT * FROM yourTable order by year,month,day DESC

  • 1 decade ago

    order by `year` DESC,`month` DESC,`day` DESC

    並沒有錯,

    你應該先看一下錯誤訊息,

    看問題錯在哪裡。

    說不定是欄位名稱打錯(大小寫)

    甚至標點符號之類的問題

    再者如果你有JOIN多個表

    也有可能是兩個表都剛好有重複的欄位所以出錯

    總之先看錯誤訊息吧

    Source(s): 經驗
Still have questions? Get your answers by asking now.