Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
基于条件的MySQL排序_Mysql - Fatal编程技术网

基于条件的MySQL排序

基于条件的MySQL排序,mysql,Mysql,我希望MySQL的一位大师能帮上忙 表如下:(为了保持简单,删除了一些列) 以下是我当前(非常简单)的查询: 这给了我 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 nin

我希望MySQL的一位大师能帮上忙

表如下:(为了保持简单,删除了一些列)

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

这给了我

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应该与正常的帖子保持一致,并按其创建日期排序,这样我就可以结束了

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语句来选择这个,我只是不知道该怎么做,帮助??:-)


默认功能顺序设置为5000的原因是,双order by将它们置于顶部,而5000的order by则置于底部。

order by不需要直接对列名进行操作,它接受一个表达式。因此,您可以创建一个表达式来对这些帖子进行排序,其中
is\u featured
和一个未来的
featured\u end
日期按
featured\u order
排序,其他则按
created\u date
排序

在本例中,我应用了第二个排序方式,即
id
,因为您没有在示例中提供
create\u date
列,但效果应该是相同的。我还添加了第四个功能(未过期),以证明它将优先于第三个功能

也许更直接的方法是在
SELECT
中应用类似的
CASE
条件,强制过期功能的
characterized\u order
值为5000,然后简单地按
order BY
排序。这更容易理解:

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

请创建一个链接,并将其链接到问题中,因为这将使人们更容易回答问题question@Alex这无关紧要,因为第二级排序不在
功能化\u顺序上。它只适用于未过期的功能,因此它可以比其他未过期的功能更高。是的,我同意这通常并不重要。但我们有一份一份的ASC订单,create_date DESC和OP writed并按其create_date排序,因此如果将
9999999
放入,则这些记录将不会按
create_date
与常规记录一起排序,而仅在themselves@Alex检查sqlfiddle-通过
id
进行的子排序实际上与
create\u date
相同,它确实有效。99999只是将它们从功能组中推出,下一个订单正常应用,无论值是多少。为了保持一致性,我将其更改为5000确实,这是因为所有与条件不匹配的行都会被分配99999(或5000),而完全不考虑它们的
featured\u order
值,因此它们都被分组在一起进行子排序。是的,我看到了您的逻辑。但是,为了尊重原始数据,我更愿意在以下情况下使用案例:is_featured=0或(is_featured=1和FROM_UNIXTIME(featured_end)>=NOW())然后选择featured_order ELSE 5000 end
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
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;
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