在 MySQL 的子查询中使用 LIMIT 是否有解决方法?
这是我原来的查询...
SELECT `id`
FROM `properties`
LIMIT 10, 20
LIMIT
条件用于分页。
现在,我必须像以前一样获取所有内容,但我只需要获取存在条件的行的三分之一。
我想出了这个,只是在我弄清楚如何做之前抛出LIMIT 30
(匹配的总行数/ 3)* 2。MySQL
SELECT `id`
FROM `properties`
WHERE `id` NOT IN (SELECT `id`
FROM `properties`
WHERE `vendor` = "abc"
ORDER BY RAND()
LIMIT 30)
LIMIT 10, 20
说...
1235 - 此版本的 MySQL 尚不支持“LIMIT &” IN/ALL/ANY/SOME 子查询'
我想我不能在子查询中使用 LIMIT
。
所以这是一个多问题,但所有相关......
- 子查询中是否有
LIMIT
的解决方法? - 我可以使用 MySQL 选择 1/3 的匹配行吗?
- 我是否需要将其变成 2 个查询,或者只选择全部并取消设置 PHP 中不需要的行?
Here is my original query...
SELECT `id`
FROM `properties`
LIMIT 10, 20
The LIMIT
condition is for pagination.
Now, I have to get all like before, but I need to get only a third of rows where a condition is present.
I came up with this, just throwing LIMIT 30
in before I figured out how to do (total rows matched / 3) * 2.
SELECT `id`
FROM `properties`
WHERE `id` NOT IN (SELECT `id`
FROM `properties`
WHERE `vendor` = "abc"
ORDER BY RAND()
LIMIT 30)
LIMIT 10, 20
MySQL said...
1235 - This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'
I guess I can't use LIMIT
in a subquery.
So this is a multi question but all related...
- Is there a workaround for
LIMIT
in subquery? - Can I select a 1/3 of matched rows with MySQL?
- Do I need to turn this into 2 queries, or just select all and unset the rows not required in PHP?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您的 MySQL 版本不支持,那么您有 2 个选择:
If your version of MySQL doesn't support that then you have 2 options:
MySQL 确实支持子查询中的 LIMIT...但 MySQL 不支持在子查询中使用 IN/NOT IN。
说实话,我真的不知道你想达到什么目的。
MySQL does support LIMIT in a subquery...but MySQL does NOT support using IN/NOT IN with a subquery.
To be honest I really don't know what you are trying to accomplish.
抱歉我迟到了,这对我有用:
Sorry I'm late, this worked for me: