Domanda

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.

È stato utile?

Soluzione

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

Altri suggerimenti

You need

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

if you want to return all reviews.

You can limit the subquery by using LIMIT

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

change your code like this : SELECT * FROM package_reviews WHERE post_id = ANY (SELECT post_id FROM wp_posts WHERE post_author=1); this link is very useful http://dev.mysql.com/doc/refman/5.0/en/subquery-errors.html

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top