SQL_CALC_FOUND_ROWS 返回错误
我使用嵌套 MySQL 查询来获取用户帖子,当我将 SQL_CALC_FOUND_ROWS 放入子查询中时,MySQL 返回错误: 'SQL_CALC_FOUND_ROWS' 的使用/放置不正确
查询是:
SELECT inner_table.*
, users.username as last_username
, posts.date_added as last_date_added
, users.user_id as last_user_id
, posts.date_added as last_post_date
FROM (
SELECT SQL_CALC_FOUND_ROWS topics.topic_id
, topics.date_added
, topics.title
, topics.user_id
, MAX(posts.post_id) as last_post
, posts.user_id as post_user_id
, users.username
, topics.previews
, topics.fcat_id
, topics.is_important
, topics.is_locked
, topics.lastpost_time
, (SELECT COUNT(*) FROM posts WHERE posts.topic_id=topics.topic_id) as posts_cnt
FROM topics
LEFT JOIN users ON (users.user_id = topics.user_id)
LEFT JOIN posts ON (topics.topic_id = posts.topic_id)
WHERE topics.user_id = ".$this->session->getSession("user_data", "user_id")."
OR ".$this->session->getSession("user_data", "user_id")."
IN (
SELECT DISTINCT user_id
FROM posts
WHERE posts.topic_id = topics.topic_id
)
GROUP BY topics.topic_id
ORDER BY topics.lastpost_time DESC
LIMIT ".$limit * $page.", ".$limit."
) as inner_table
LEFT JOIN `posts` ON (posts.post_id=inner_table.last_post)
LEFT JOIN `users` ON (users.user_id=posts.user_id)
ORDER BY inner_table.lastpost_time DESC
I use nested MySQL query to get user posts, and when I put SQL_CALC_FOUND_ROWS in child query, MySQL return error: Incorrect usage/placement of 'SQL_CALC_FOUND_ROWS'
The query is:
SELECT inner_table.*
, users.username as last_username
, posts.date_added as last_date_added
, users.user_id as last_user_id
, posts.date_added as last_post_date
FROM (
SELECT SQL_CALC_FOUND_ROWS topics.topic_id
, topics.date_added
, topics.title
, topics.user_id
, MAX(posts.post_id) as last_post
, posts.user_id as post_user_id
, users.username
, topics.previews
, topics.fcat_id
, topics.is_important
, topics.is_locked
, topics.lastpost_time
, (SELECT COUNT(*) FROM posts WHERE posts.topic_id=topics.topic_id) as posts_cnt
FROM topics
LEFT JOIN users ON (users.user_id = topics.user_id)
LEFT JOIN posts ON (topics.topic_id = posts.topic_id)
WHERE topics.user_id = ".$this->session->getSession("user_data", "user_id")."
OR ".$this->session->getSession("user_data", "user_id")."
IN (
SELECT DISTINCT user_id
FROM posts
WHERE posts.topic_id = topics.topic_id
)
GROUP BY topics.topic_id
ORDER BY topics.lastpost_time DESC
LIMIT ".$limit * $page.", ".$limit."
) as inner_table
LEFT JOIN `posts` ON (posts.post_id=inner_table.last_post)
LEFT JOIN `users` ON (users.user_id=posts.user_id)
ORDER BY inner_table.lastpost_time DESC
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为内部查询不允许使用
SQL_CALC_FOUND_ROWS
。相反,放入外部查询。
另外,最后一行有错误:
将其替换为:
更新
为了回应您的评论,请提出以下解决方法。
将内部选择输出到临时表中。
使用这样的代码:
使用 SQL_CALC_FOUND_ROWS 也不适用于 INSERT .. SELECT ,但上面的代码是一种解决方法,您当然可以使用
LIMIT 1
单独运行内部查询让它尽可能快。I don't think
SQL_CALC_FOUND_ROWS
is allowed on an inside query.Put in on the outer query instead.
Also you have an error in the last line:
Replace it with:
UPDATE
In response to your comment the following work-around ideas.
Output the inner select into a temp table.
Using code like this:
Using SQL_CALC_FOUND_ROWS doesn't work on an INSERT .. SELECT either, but the above code is a workaround, you can of course run the inner query separate with a
LIMIT 1
on it to make it as fast as possible.