MySQL排序条件

时间:2015-04-15 18:25:18

标签: mysql

我希望其中一位MySQL专家可以帮助解决这个问题......

这是表格:(为了保持这一点,删除了一些列)

+------------------------------------------+------------------------------+-------------------+-------------------------+-----------------
| id       | text                         | is_featured       | featured_order          | featured_start          | featured_end          |
| 1        | This is my first post        |                 0 |                    5000 |                       0 |                     0 |
| 2        | This is my second post       |                 0 |                    5000 |                       0 |                     0 |
| 3        | This is my third post        |                 0 |                    5000 |                       0 |                     0 |
| 4        | FEATURED #3                  |                 1 |                       3 |              1427846400 |            1429488000 |
| 5        | FEATURED #1                  |                 1 |                       1 |              1427846400 |            1428192000 |
| 6        | This is my fourth post       |                 0 |                    5000 |                       0 |                     0 |
| 7        | FEATURED #2                  |                 1 |                       2 |              1427846400 |            1428624000 |
| 8        | This is my fifth post        |                 0 |                    5000 |                       0 |                     0 |
| 9        | This is my sixth post        |                 0 |                    5000 |                       0 |                     0 |
| 10       | This is my seventh post      |                 0 |                    5000 |                       0 |                     0 |
| 11       | This is my eighth Post       |                 0 |                    5000 |                       0 |                     0 |
| 12       | This is my ninth post        |                 0 |                    5000 |                       0 |                     0 |

这是我当前(非常简单)的查询:

SELECT id, text
FROM posts
WHERE visible = 1
ORDER BY featured_order ASC, create_date DESC
LIMIT $offset, $limit;

这给了我

FEATURED #1
FEATURED #2
FEATURED #3
This is my first
This is my second
This is my third
This is my fourth
This is my fifth
This is my sixth
This is my seventh
This is my eight
This is my ninth
This is my tenth

但你会注意到特色#1和特色#2有过期时间戳(分别是4月5日和10日) - 特色#3将于4月20日到期......

所以特征#1和#2应该与普通帖子一致,并按照他们的create_date进行排序,以便我最终得到。

FEATURED #3
This is my first
This is my second
This is my third
FEATURED #1
This is my fourth
FEATURED #2
This is my fifth
This is my sixth
This is my seventh
This is my eight
This is my ninth
This is my tenth

我知道我可以在MySQL中做一个IF语句来选择这个,我只是不知道怎么做,HELP ??? : - )

默认feature_order设置为5000的原因是因为双顺序,将它们置于顶部,5000位于底部。

1 个答案:

答案 0 :(得分:2)

ORDER BY不需要直接对列名进行操作,它接受表达式。因此,您可以制作一个表达式,对那些is_featured和未来featured_end日期按featured_order排序的帖子进行排序,其他人按created_date排序。

Here it is in action at SQLFiddle ...

SELECT id, text
FROM posts
ORDER BY 
  -- Non-expired, featured posts are sorted by featured_order
  -- Expired ones are just given a high value to sort later
  CASE WHEN (is_featured=1 AND FROM_UNIXTIME(featured_end) >= NOW()) THEN featured_order ELSE 5000 END,
  -- Followed by other column sorts 
  create_date DESC
LIMIT $offset, $limit;

在示例中,我按id应用了第二种排序,因为您未在示例中提供create_date列,但效果应该相同。我添加了第四个功能(非过期),以证明它将排在第三个功能。

或许更简单的方法是在CASE中应用类似的SELECT条件来强制过期要素的featured_order值为5000,然后只需对{ORDER BY中的值进行排序1}}。这更容易理解:

SELECT 
  id, text, 
  -- A conditional column to force expired features 
  -- to have 5000, the same as non-featured posts
  CASE 
    WHEN is_featured = 1 AND FROM_UNIXTIME(featured_end) >= NOW() THEN featured_order
    ELSE 5000
  END as calc_featured_order
FROM posts
ORDER BY 
  -- Sort on the conditional column
  calc_featured_order,
  -- Then other columns
  create_date DESC

Example in action...