0
SELECT 
    D.Name,
    GROUP_CONCAT((SELECT 
                C.Name
            FROM
                County AS C
                    INNER JOIN
                DealerCounty AS DC ON DC.CountyId = C.CountyId
                WHERE DC.DealerId = D.DealerId))
FROM
    Dealer AS D

如您所见,我想按经销商对县名进行分组。它返回,Error Code: 1242. Subquery returns more than 1 row 我应该如何更改我的查询?

4

4 回答 4

2

一些微调以及更正:

SELECT 
    D.Name,
    GROUP_CONCAT(c.name)
FROM
    Dealer AS D
        JOIN County AS C ON DC.DealerId = D.DealerId
        JOIN DealerCounty AS DC ON DC.CountyId = C.CountyId
GROUP BY 
    D.name
于 2013-05-23T13:48:56.723 回答
1

您缺少以下group by声明:

SELECT  D.Name,
        GROUP_CONCAT((SELECT C.Name
                      FROM County AS C INNER JOIN
                           DealerCounty AS DC
                     ON DC.CountyId = C.CountyId))
FROM Dealer AS D
group by Name;

但是,这个版本的查询要简单得多:

select D.Name,
       group_concat(C.Name)
from Dealer D join
     County C
     on D.CountyId = C.CountryId
group by D.Name
于 2013-05-23T13:46:54.493 回答
0

您需要添加Group by D.Name

SELECT 
    D.Name,
    GROUP_CONCAT((SELECT 
                C.Name
            FROM
                County AS C
                    INNER JOIN
                DealerCounty AS DC ON DC.CountyId = C.CountyId
                WHERE DC.DealerId = D.DealerId))
FROM
Dealer AS D
group by D.Name
于 2013-05-23T13:46:19.790 回答
0

尝试添加聚合函数GROUP BY

SELECT D.Name,
GROUP_CONCAT((SELECT C.Name
        FROM County AS C 
        INNER JOIN DealerCounty AS DC 
        ON DC.CountyId = C.CountyId
            ))
FROM Dealer AS D
GROUP BY D.Name
于 2013-05-23T13:47:22.997 回答