2011-09-21 80 views
2

我有以下查询:MySQL的子查询返回多个1行

(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) 

但是,当我在phpMyAdmin执行这个查询它会显示一个错误messgae:

#1242 - Subquery returns more than 1 row 

哪些错误查询。请帮忙。提前致谢。

回答

2

我认为(部分)问题是您在错误的位置重新使用别名。
您应该使用别名一次,并且不要将它们混合用于同一表的不同实例。

(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) 

因此,对每个xcart_extra_field_values表实例使用一个唯一的别名efvx。

+0

Thakns为您解答。这是问题所在,我为两个子查询使用了相同的别名。 –

0

在phpmyadmin中手动执行每个子查询,并查看哪一个返回了多行,并且您有答案。另一个选项是为每个子查询添加1的限制,以确保只返回一行。当然,最好找到bug并解决它。