mysql按日期排序获得10个结果,然后再按id排序

时间:2012-02-17 16:10:54

标签: mysql sql-order-by

在我当前的方法中,我在sql查询中使用order by date,然后将back results设置为新数组,通过php按ID排序数组。

SELECT * FROM table 
WHERE 
(
MATCH (title,content) 
AGAINST ('+$search' IN BOOLEAN MODE)
) 
order by date DESC 
limit 0,10

我想问一下,是否有一个sql方法,首先order by date在这10个结果中得到10个结果,再次order by idjust in one sql query

3 个答案:

答案 0 :(得分:10)

围绕当前查询包装外部查询。

SELECT q.* 
    FROM (SELECT * FROM table 
          WHERE 
          (
          MATCH (title,content) 
          AGAINST ('+$search' IN BOOLEAN MODE)
          ) 
          order by date DESC
          limit 0,10) q
   ORDER BY q.id

答案 1 :(得分:0)

这可能有效:

SELECT * FROM (
  SELECT * FROM table
  WHERE ...
  ORDER BY date DESC
  LIMIT 10
)
ORDER BY id

答案 2 :(得分:0)

只需输入逗号,然后按另一列排序:

SELECT * FROM table
WHERE 
(MATCH (title,content) 
AGAINST ('+$search' IN BOOLEAN MODE)) 
order by date, id DESC 
limit 0,10