samir chauhan
samir chauhan

Reputation: 1543

mysql subquery returns more than 1 row

I have the following query :

(select xyz.* from 
      (select xt.image, p.*, pc.categoryid, c.category, 
          (select value from xcart_extra_field_values 
             where efv.productid=p.productid and efv.fieldid = 2) as Type,
          (select value from xcart_extra_field_values 
             where efv.productid=p.productid and efv.fieldid = 1) as Zone 
       FROM xcart_products p 
       inner join xcart_products_categories pc 
         on p.productid=pc.productid 
       inner join xcart_categories c 
         on pc.categoryid=c.categoryid 
       inner join xcart_extra_field_values efv 
         on p.productid=efv.productid 
       inner join xcart_images_T xt 
         on p.productid=xt.id) xyz 
       where categoryid='1' and Type='2' and Zone='1' 
       group by productid)

But when I execute this query in phpmyadmin it displays a error messgae :

#1242 - Subquery returns more than 1 row

Whats wrong in the query.Please help. Thanks in advance.

Upvotes: 2

Views: 1236

Answers (2)

Johan
Johan

Reputation: 76567

I think (part of) the problem is that you are re-using an alias in a wrong location.
You should one use an alias once, and don't mix them for different instances of the same table.

(select xyz.* from 
  (select xt.image, p.*, pc.categoryid, c.category, 
      (select value from xcart_extra_field_values efv2 
         where efv2.productid=p.productid and efv2.fieldid = 2) as Type,
      (select value from xcart_extra_field_values efv3
         where efv3.productid=p.productid and efv3.fieldid = 1) as Zone 
   FROM xcart_products p 
   inner join xcart_products_categories pc 
     on p.productid=pc.productid 
   inner join xcart_categories c 
     on pc.categoryid=c.categoryid 
   inner join xcart_extra_field_values efv1 
     on p.productid=efv1.productid 
   inner join xcart_images_T xt 
     on p.productid=xt.id) xyz 
   where categoryid='1' and Type='2' and Zone='1' 
   group by productid)

So use a unique alias efvx for each xcart_extra_field_values table instance.

Upvotes: 2

Bas Slagter
Bas Slagter

Reputation: 9929

Execute each subquery manually in phpmyadmin and see which one is returning more than one row and you have your answer. Another option is to add a limit of 1 to each subquery to make sure only one row is returned. Of course it is better to look for the bug and solve it.

Upvotes: 0

Related Questions