duancai7002 2011-05-21 17:28
浏览 256

SQL_CALC_FOUND_ROWS返回错误

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
  • 写回答

1条回答 默认 最新

  • duanhongyi2964 2011-05-21 20:57
    关注

    I don't think SQL_CALC_FOUND_ROWS is allowed on an inside query.
    Put in on the outer query instead.

    SELECT SQL_CALC_FOUND_ROWS inner_table.*
      ....
    

    Also you have an error in the last line:

    ORDER BY inner_table.lastpost_time DES
    

    Replace it with:

    ORDER BY inner_table.lastpost_time DESC
    

    UPDATE

    In response to your comment the following work-around ideas.

    Output the inner select into a temp table.

    Using code like this:

    /* do this once*/
    CREATE TABLE `temp_table` LIKE SELECT topics.topic_id
          , topics.date_added ....
    
    /*do this for every query*/
    DELETE FROM temp_table WHERE temp_table.topic_id <> 0;
    INSERT INTO temp_table SELECT topics.topic_id
      , topics.date_added
      , topics.title
    .... /*!! without the limit clause !!*/
    SELECT count(*) as rowcount FROM temp_table;
    
    /*then use the temp_table in place of the inner select*/
    
    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 temp_table .....
    LIMIT ....
    

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示