I am having a SQL_CALC_FOUND_ROWS error, what to do?

Hi there,

I am only having 1 error in my error_log, really lost with this error. I do know that I need to disable plugins, but I have 15 plugins and I get this error random. Anyone here can direct me please what to do or where to look at. Thank you.

[06-Jun-2016 23:46:12 UTC] WordPress-databasefejl Not unique table/alias: ‘wp_comments’ for forespørgslen SELECT SQL_CALC_FOUND_ROWS wp_posts.*, AVG( wp_commentmeta.meta_value ) as average_rating , AVG( wp_commentmeta.meta_value ) as average_rating FROM wp_posts INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id ) INNER JOIN wp_postmeta AS mt1 ON ( wp_posts.ID = mt1.post_id )
LEFT OUTER JOIN wp_comments ON(wp_posts.ID = wp_comments.comment_post_ID)
LEFT JOIN wp_commentmeta ON(wp_comments.comment_ID = wp_commentmeta.comment_id)

        LEFT OUTER JOIN wp_comments ON(wp_posts.ID = wp_comments.comment_post_ID)
        LEFT JOIN wp_commentmeta ON(wp_comments.comment_ID = wp_commentmeta.comment_id)
     WHERE 1=1  AND ( 

wp_term_relationships.term_taxonomy_id IN (113)
) AND (
( wp_postmeta.meta_key = ‘_visibility’ AND CAST(wp_postmeta.meta_value AS CHAR) IN (‘visible’,‘catalog’) )
AND
( mt1.meta_key = ‘_visibility’ AND CAST(mt1.meta_value AS CHAR) IN (‘visible’,‘catalog’) )
) AND wp_posts.post_type = ‘product’ AND (wp_posts.post_status = ‘publish’) AND ( wp_commentmeta.meta_key = ‘rating’ OR wp_commentmeta.meta_key IS null ) AND ( wp_commentmeta.meta_key = ‘rating’ OR wp_commentmeta.meta_key IS null ) GROUP BY wp_posts.ID ORDER BY average_rating DESC, wp_posts.post_date DESC LIMIT 0, 16 fra require(‘wp-blog-header.php’), wp, WP->main, WP->query_posts, WP_Query->query, WP_Query->get_posts

what to do? why, fix it, of course :wink:

the reason you are getting this error –

is because you have two tables in your query called wp_comments

also, do yourself a huge favour, learn to format your sql…

SELECT SQL_CALC_FOUND_ROWS 
       wp_posts.*
     , AVG( wp_commentmeta.meta_value ) as average_rating 
     , AVG( wp_commentmeta.meta_value ) as average_rating 
  FROM wp_posts 
INNER 
  JOIN wp_term_relationships 
    ON (wp_posts.ID = wp_term_relationships.object_id) 
INNER 
  JOIN wp_postmeta 
    ON ( wp_posts.ID = wp_postmeta.post_id ) 
INNER 
  JOIN wp_postmeta AS mt1 
    ON ( wp_posts.ID = mt1.post_id )
LEFT OUTER 
  JOIN wp_comments 
    ON(wp_posts.ID = wp_comments.comment_post_ID)
LEFT 
  JOIN wp_commentmeta 
    ON(wp_comments.comment_ID = wp_commentmeta.comment_id)
LEFT OUTER 
  JOIN wp_comments 
    ON(wp_posts.ID = wp_comments.comment_post_ID)
LEFT 
  JOIN wp_commentmeta 
    ON(wp_comments.comment_ID = wp_commentmeta.comment_id)
 WHERE 1=1  
   AND ( wp_term_relationships.term_taxonomy_id IN (113) ) 
   AND ( 
       ( wp_postmeta.meta_key = 'visibility' 
   AND CAST(wppostmeta.meta_value AS CHAR) IN ('visible' , 'catalog') 
       ) 
   AND ( mt1.meta_key = 'visibility' 
   AND CAST(mt1.metavalue AS CHAR) IN ('visible' , 'catalog') 
       )
       ) 
   AND wp_posts.post_type = 'product' 
   AND (wp_posts.post_status = 'publish') 
   AND ( wp_commentmeta.meta_key = 'rating' 
      OR wp_commentmeta.meta_key IS null ) 
   AND ( wp_commentmeta.meta_key = 'rating' 
      OR wp_commentmeta.meta_key IS null ) 
GROUP 
    BY wp_posts.ID 
ORDER 
    BY average_rating DESC
     , wp_posts.post_date DESC LIMIT 0
     , 16 fra require('wp-blog-header.php')
     , wp
     , WP->main
     , WP->query_posts
     , WP_Query->query
     , WP_Query->get_posts

Thank you. The layout looks better and easier to read. Well I am a total noob in sql. What is the best way to remove the WP_comments in phpmyadmin? Just selecting it and delete it? Or will it work if I use this plugin and optimise the database: https://nl.wordpress.org/plugins/wp-optimize/

sorry, i cannot comment on plugins

do you understand why you’re getting that error in your query?

Yes, every table needs a unique key right. Wp_comments is not unique, so there is another one. That is interfering. That’s what I am understanding. Am I right? But when I look into it, I don’t see more wp_comments tables

in the query i posted above, line 16 and line 22

you will also get an error for line 19 and line 25

who wrote the sql for this query, you or a plugin?

Ok now I do understand it a bit more. So wp_comment and wp_commentmeta. A plugin. If I know which plugin this is causing, I can sent a message to the developer so he will able to fix it.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.