这是我的SQL语句:

(select * from items
where items.created > curdate() - interval 2 week
order by items.created desc limit 0,10000000000)

union all

(select * from items
where items.created < curdate() - interval 2 week
order by items.popularity desc limit 0,15)


我试图找出一种方法来将整个查询结果限制为一定数量(例如25)。现在,此结果为第一个结果返回一个无限制的数字(这是我想要的),然后为第二个结果返回15。我希望能够限制整个查询,以便即使第一个结果返回8,第二个结果返回17,总计25。

我相信要这样做,我必须在第一个查询中以某种方式使用count(),然后从我想要的总数中减去它,并将该数字用作第二个查询的限制。我不知道这是怎么做到的。

提前致谢!

最佳答案

另一种选择:

select * from
(

  (select * from items
  where items.created > curdate() - interval 2 week
  order by items.created desc limit 0,10000000000)

  union all

  (select * from items
  where items.created < curdate() - interval 2 week
  order by items.popularity desc)

) uniond_tables_alias
limit 25


uniond_tables_alias是联合部分的别名,您可以选择所需的任何名称。

10-05 20:33
查看更多