计算单个语句中第二个查询中第一个查询的 SQL 结果

发布于 2024-12-11 23:24:04 字数 533 浏览 0 评论 0原文

这是我的 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(),然后减去从我想要的总数中取出该数字,并将该数字用作第二个查询的限制。我不知道这是怎么做到的。

提前致谢!

Here's my SQL statement:

(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)

I'm trying to figure out a way to limit the entire result of the query to a certain number (say 25). As it is now, this result returns an unlimited number for the first result (which is what I want), then returns 15 for the second result. I want to be able to limit the whole query so that even if the first result returns 8, the second result returns 17, total 25.

I believe to do this, I have to use count() somehow in the first query, then subtract that from the total I want and use that number as the 2nd query's limit. I have no idea how this is done.

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

嘿嘿嘿 2024-12-18 23:24:04

这是所需的查询 -

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)) t
limit 0,25

Here is the required query -

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)) t
limit 0,25
莫言歌 2024-12-18 23:24:04

另一个选择:

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是uniond部分的别名,您可以选择任何您想要的名称。

Another select:

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

The uniond_tables_alias is an alias for the uniond section, you can choose any name you want.

迷鸟归林 2024-12-18 23:24:04

不需要嵌套查询,只需执行以下操作:

(select * from items
where items.created > curdate() - interval 2 week 
order by items.created desc)                      # remove LIMIT here

UNION ALL

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

LIMIT 25;                                         # add LIMIT here

如果至少有 25 个,则将返回第一个 SELECT 的前 25 个结果。否则,它将用第二个 SELECT 结果填充剩余结果,直到达到 25 的限制。

No need for nested queries, simply do:

(select * from items
where items.created > curdate() - interval 2 week 
order by items.created desc)                      # remove LIMIT here

UNION ALL

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

LIMIT 25;                                         # add LIMIT here

This'll return the 25 first results from the first SELECT if there's at least 25. Otherwise it will fill up the remaining results with the second SELECT results until the limit of 25 is reached.

别闹i 2024-12-18 23:24:04
SELECT 1 AS sortkey, * from items ....
UNION ALL
SELECT 2 AS sortkey, * from items ....

ORDER BY sortkey, etc.
LIMIT 25
SELECT 1 AS sortkey, * from items ....
UNION ALL
SELECT 2 AS sortkey, * from items ....

ORDER BY sortkey, etc.
LIMIT 25
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文