使用 LIMIT 时,在 MySQL 中使用 UNION ALL 获取各个 SELECT 语句的结果行计数
我需要获取使用 LIMIT 的 UNION ALL SELECT 查询中各个 SELECT 语句的行数。 MySQL 文档非常清楚如何获取查询的全局行数(将各个 SELECT 放在括号中,并仅在 first 语句中放置 SQL_CALC_FOUND_ROWS,然后获取 FOUND_ROWS()通常的方式)。但是,我还需要各个 SELECT 语句的结果行计数。简化的查询:
(SELECT SQL_CALC_FOUND_ROWS `field1`,`field2` FROM `db`.`table1` WHERE `id`>1000)
UNION ALL
(SELECT `field1`,`field2` FROM `db`.`table2` WHERE `id`>1000)
UNION ALL
...
(SELECT `field1`,`field2` FROM `db`.`tableN` WHERE `id`>1000)
LIMIT 0,10
如果将 SQL_CALC_FOUND_ROWS 放置在每个 SELECT 语句中,则会发出“'SQL_CALC_FOUND_ROWS' 的使用/放置不正确”错误。 谷歌了很多,在这里阅读相关消息,无济于事。也许事情很简单,我只是无法理解它。
I need to get the row count for individual SELECT statements in an UNION ALL SELECT query that uses LIMIT. The MySQL docs are pretty clear about getting the global row count for the query (place individual SELECTs in parenthesis and place an SQL_CALC_FOUND_ROWS only in the first statement, then get FOUND_ROWS() the usual way). However, I also need the result row count for the individual SELECT statements. The query, simplified:
(SELECT SQL_CALC_FOUND_ROWS `field1`,`field2` FROM `db`.`table1` WHERE `id`>1000)
UNION ALL
(SELECT `field1`,`field2` FROM `db`.`table2` WHERE `id`>1000)
UNION ALL
...
(SELECT `field1`,`field2` FROM `db`.`tableN` WHERE `id`>1000)
LIMIT 0,10
If SQL_CALC_FOUND_ROWS is placed in every SELECT statement an "Incorrect usage/placement of 'SQL_CALC_FOUND_ROWS'" error is issued.
Google this a lot, read related messages here, to no avail. Might be something really simple, I just can't get my mind around it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您收到该错误是因为 SQL_CALC_FOUND_ROWS 可用于查询以将单个数字返回到 FOUND_ROWS() 中。
您可以这样做来获取计数:
如果有帮助的话。
如果您想从表中返回带有计数的行,请修改它:
从您的问题来看,您并不清楚您想要实现的目标。
You're getting that error because SQL_CALC_FOUND_ROWS can be used on a query to return a single number into FOUND_ROWS().
You can get back counts doing this:
if that helps.
If you want to return the rows from the tables with a count then modify it:
From your question, it's not precisely clearly what you're trying to achieve.