Order By Sum of Two Fields

后端 未结 3 1519
天涯浪人
天涯浪人 2021-01-02 05:43

Let\'s say I have a table with karma_up and karma_down. Everytime someone votes up karma_up gets incremented and everytime someone votes down karma_down gets incremented on

相关标签:
3条回答
  • 2021-01-02 06:10

    Does this work? If not, could you include the results in your question? Ordering on an expression ought to work as expected.

    SELECT `post_id`, `karma_up`, `karma_down`, `karma_up` - `karma_down` AS `total`
    ORDER BY `total` DESC
    
    0 讨论(0)
  • 2021-01-02 06:29
    SELECT *, karma_up - karma_down AS karma_total 
    FROM MyTable
    ORDER BY karma_total DESC;
    
    0 讨论(0)
  • 2021-01-02 06:31

    Very simple

    SELECT 
    ID, KARMA_UP, KARMA_DOWN, (KARMA_UP-KARMA_DOWN) AS USER_KARMA 
    FROM KARMA 
    ORDER BY USER_KARMA DESC
    
    0 讨论(0)
提交回复
热议问题