Search code examples
mysqlsqlmysql-error-1242

how to handle subquery which returns more than one row


This is the query that I have written to fetch all reviews of Author-1. Author-1 has written 2 posts each having many reviews. I want all those reviews.

  SELECT * 
  FROM `package_reviews`
  WHERE `post_id` = (SELECT `post_id` 
                     FROM `wp_posts` 
                     WHERE `post_author`=1);

Getting the error message as #1242 - Subquery returns more than 1 row.


Solution

  • You should use join here.

    Something like this.

    SELECT * FROM `package_reviews` r LEFT JOIN `wp_posts` p ON r.post_id= p.post_id WHERE p.post_author = 1