简体   繁体   English

Mysql ORDER BY多列IF AND

[英]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 经过复杂的SELECT状态后,该表看起来像这样:$ 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. 如果NumLikes等于0,则SeenLike列为空。 The PostSeen will always be 0 or 1. Ive put the date in a simplified format here, but its proper DateTimed. PostSeen始终为0或1。我在此处以简化格式输入了日期,但使用了正确的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. 我已经尝试过IF和CASE,但到目前为止都没有用。 Would appreciate an explanation to any answer as well! 还要感谢对任何答案的解释! Thanks for any help! 谢谢你的帮助!

You could create two subqueries and then UNION them. 您可以创建两个子查询,然后对其进行UNION。 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'. 第一个子查询将具有“ WHERE FromUserID = $ myid AND NumLikes> 0 ORDER BY SeenLike ASC,日期DESC”和第二个“ WHERE FromUserID!= $ myid ORDER BY PostSeen ASC,日期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 但是,您可以在order子句中使用任何类型的表达式,如布尔值或整数

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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