简体   繁体   中英

MySql ORDER BY multiple columns IF AND

So I've joined two tables for a particular query. After a complicated SELECT statemnt, the table would look vaguely like this: $myid = 3

PostID | FromUserID | To User ID|Date     | PostSeen | NumLikes  |SeenLike
61     |     48     |     3     | 5/11/13 |    1     |     1     |  1
59     |     3      |     3     | 4/11/13 |    1     |     1     |  0
58     |     3      |     3     | 4/11/13 |    1     |     1     |  1
25     |     47     |     3     | 3/11/13 |    1     |     2     |  0
53     |     56     |     3     | 2/11/13 |    0     |     3     |  1
21     |     3      |     55    | 1/11/13 |    1     |     0     |  null
20     |     56     |     3     | 30/10/13|    1     |     0     |  null
18     |     47     |     3     | 29/10/13|    0     |     0     |  null

If NumLikes is equal to 0 then the SeenLike column is null. The PostSeen will always be 0 or 1. Ive put the date in a simplified format here, but its proper DateTimed.

Essentially, I want do something that is the equivalent of

if(FromUserID == $myid && NumLikes > 0){
ORDER BY SeenLike ASC, Date DESC
}
if (FromUserID != $myid) {
ORDER BY PostSeen ASC, Date DESC
}

(I know this is totally wrong, its just to illustrate vaguely what I'm aiming for) So that it would order the results something like the following:

PostID | FromUserID | To User ID|Date     | PostSeen | NumLikes  |SeenLike
59     |     3      |     3     | 4/11/13 |    1     |     1     |  0
25     |     47     |     3     | 3/11/13 |    1     |     2     |  0
53     |     56     |     3     | 2/11/13 |    0     |     3     |  1
18     |     47     |     3     | 29/10/13|    0     |     0     |  null
61     |     48     |     3     | 5/11/13 |    1     |     1     |  1
58     |     3      |     3     | 4/11/13 |    1     |     1     |  1
21     |     3      |     55    | 1/11/13 |    1     |     0     |  null
20     |     56     |     3     | 30/10/13|    1     |     0     |  null

How would I go about doing this? I've tried IF's and CASE's but none have worked so far. Would appreciate an explanation to any answer as well! Thanks for any help!

You could create two subqueries and then UNION them. The first subquery would have 'WHERE FromUserID = $myid AND NumLikes > 0 ORDER BY SeenLike ASC, Date DESC' and the second 'WHERE FromUserID != $myid ORDER BY PostSeen ASC, Date DESC'.

You cannot order half the results by one clause and the rest by another clause. If you think about it, it doesn't make any sense.

However, you can use any type of expression that evaluates like a boolean or an integer in the order clause

for example

ORDER BY FromUserID = $myid desc, -- this will be true or false depending on the fromuserid
    (FromUserID = $myid)*SeenLike ASC, --this will be 0 if fromuser is not equal to myid or =seenlike otherwise
    (FromUserID != $myid)*PostSeen ASC, -- and so on
    Date DESC

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM