如何在其他查询MySQL中使用select子查询

时间:2017-03-29 19:03:58

标签: mysql

我正在编写一个mysql select子查询,即工作正常,它返回2列是否有方法只选择1列。 我的问题是

SELECT sum(fl.qunt) as qunt,(
    SELECT GROUP_CONCAT( xp.id
    SEPARATOR  ',' ) 
    FROM prdt AS xp
    LEFT JOIN prdt_fac AS pf ON pf.fk_product_children = xp.rowid
    WHERE pf.prdt_fat = p.id
    AND pf.prdt_ch = 6953
    GROUP BY pf.prdt_fat
    LIMIT 0 , 1
    ) AS prdt_chd
    FROM fac_log AS fl
    LEFT JOIN fac AS f ON fl.fac = f.id
    LEFT JOIN prdt AS p ON f.prdt = p.id
    GROUP BY prod_child
    ORDER BY fl.tms DESC 
    LIMIT 0 , 1 

返回两列qunt和prdt_ch。 但我只想在结果qunt中使用列。 有没有办法,因为必须使用内部选择查询来获得正确的结果。 这个查询的主要目的是我必须在另一个查询中使用此查询作为子查询,在这种情况下它会抛出错误“操作数应该包含1列” 提前致谢

1 个答案:

答案 0 :(得分:1)

只需使用您的选择查询选择qunt作为表(t)

  select qunt from ( 
        SELECT sum(fl.qunt) as qunt,(
            SELECT GROUP_CONCAT( xp.id
            SEPARATOR  ',' ) 
            FROM prdt AS xp
            LEFT JOIN prdt_fac AS pf ON pf.fk_product_children = xp.rowid
            WHERE pf.prdt_fat = p.id
            AND pf.prdt_ch = 6953
            GROUP BY pf.prdt_fat
            LIMIT 0 , 1
            ) AS prdt_chd
            FROM fac_log AS fl
            LEFT JOIN fac AS f ON fl.fac = f.id
            LEFT JOIN prdt AS p ON f.prdt = p.id
            GROUP BY prod_child
            ORDER BY fl.tms DESC  
      LIMIT 0 , 1 ) t