sql在subselect中选择多行

时间:2013-01-15 20:25:08

标签: mysql subquery

首先,我知道目前这是不正确的。我知道这是因为它不起作用。所以说,如果有任何帮助让我更接近我想要的结果,我们表示赞赏。

我有一个问题:

$query = "SELECT cat.*, 
       prod.title                            AS product, 
       cat1.title                            AS category, 
       (SELECT cat2.* 
        FROM   ic_store_catalog AS cat2 
        WHERE  cat2.parentid = cat.parentid) AS children 
FROM   ic_store_catalog AS cat 
       LEFT JOIN ic_products AS prod 
              ON cat.productid = prod.productid 
       LEFT JOIN ic_product_categories AS cat1 
              ON cat.categoryid = cat1.categoryid 
WHERE  `storeid` = $storeid 
       AND `column` = $column 
       AND `parentid` = 0 
ORDER  BY `the_order` ASC";

这个错误让我:Operand should contain 1 column(s)成为问题。这存在于子选择保证中。

现在我在print_r中使用PHP时需要实现的是Array与此类似的结构:

[0] => Array
            (
                [catalogID] => 165
                [storeID] => 0
                [categoryID] => 7
                [parentID] => 0
                [productID] => 4
                [title] => 
                [column] => 1
                [the_order] => 1
                [cat_order] => 1
                [category] => Cookies & Brownies
                [children] => Array 
                (
                  [0] => Array (
                      [catalogID] => 166
                      [storeID] => 0
                      [categoryID] => 8
                      [parentID] => 7
                      [productID] => 5
                      [the_order] => 1
                      [cat_order] => 1
                      [category] => Brownies
                  )
                )
            )

感谢您帮助我更接近这一结果。

1 个答案:

答案 0 :(得分:0)

(SELECT cat2.* FROM ic_store_catalog AS cat2 WHERE cat2.parentid = cat.parentid)

您应该只将cat2.*替换为代表children的一列。