使用 LIMIT 时,在 MySQL 中使用 UNION ALL 获取各个 SELECT 语句的结果行计数

发布于 2024-08-02 10:27:31 字数 666 浏览 5 评论 0原文

我需要获取使用 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 技术交流群。

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

发布评论

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

评论(1

£冰雨忧蓝° 2024-08-09 10:27:31

您收到该错误是因为 SQL_CALC_FOUND_ROWS 可用于查询以将单个数字返回到 FOUND_ROWS() 中。

您可以这样做来获取计数:

SELECT 'table1' AS tablename, COUNT(1) AS rowcount FROM table1 WHERE id > 1
UNION ALL
SELECT 'table2' AS tablename, COUNT(1) AS rowcount FROM table2 WHERE id > 1
...
SELECT 'tableN' AS tablename, COUNT(1) AS rowcount FROM tableN WHERE id > 1

如果有帮助的话。

如果您想从表中返回带有计数的行,请修改它:

SELECT field1, field2, (SELECT COUNT(1) FROM table1 WHERE id > 1000) FROM table1 WHERE id > 1000
UNION ALL
SELECT field1, field2, (SELECT COUNT(1) FROM table2 WHERE id > 1000) FROM table2 WHERE id > 1000
...
SELECT field1, field2, (SELECT COUNT(1) FROM tableN WHERE id > 1000) FROM tableN WHERE id > 1000

从您的问题来看,您并不清楚您想要实现的目标。

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:

SELECT 'table1' AS tablename, COUNT(1) AS rowcount FROM table1 WHERE id > 1
UNION ALL
SELECT 'table2' AS tablename, COUNT(1) AS rowcount FROM table2 WHERE id > 1
...
SELECT 'tableN' AS tablename, COUNT(1) AS rowcount FROM tableN WHERE id > 1

if that helps.

If you want to return the rows from the tables with a count then modify it:

SELECT field1, field2, (SELECT COUNT(1) FROM table1 WHERE id > 1000) FROM table1 WHERE id > 1000
UNION ALL
SELECT field1, field2, (SELECT COUNT(1) FROM table2 WHERE id > 1000) FROM table2 WHERE id > 1000
...
SELECT field1, field2, (SELECT COUNT(1) FROM tableN WHERE id > 1000) FROM tableN WHERE id > 1000

From your question, it's not precisely clearly what you're trying to achieve.

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