首先选择条件记录,剩余的是mysql中的第二个

时间:2014-09-20 07:05:58

标签: mysql

我在mysql表中有三列

  1. ID
  2. pubdate的
  3. 状态
  4. 我想按以下顺序显示记录

    首先显示那些

    的记录
    pubdate >=currdate() and status=1 and order by pubdate ASC
    

    然后表中没有任何条件的所有剩余记录。

    请帮助mysql查询

2 个答案:

答案 0 :(得分:0)

我试试:

    SELECT * FROM (SELECT * FROM mytable WHERE pubdate>=CURDATE() AND status=1 ORDER BY pubdate ASC) AS a
      UNION all
    SELECT * FROM (SELECT * FROM mytable WHERE pubdate<CURDATE() OR status!=1) AS b; 

答案 1 :(得分:0)

在满足条件时给予一些权重,并在ORDER BY中使用它:

SELECT *,
    IF(pubdate >= CURRENT_DATE AND status = 1, 10, 0) weight
FROM
    thetable
ORDER BY weight DESC, pubdate ASC