Mysql 在SQL查询中返回多个列

Mysql 在SQL查询中返回多个列,mysql,sql,pivot,Mysql,Sql,Pivot,我需要创建一个查询来返回多个值​,并将其作为结果列返回,例如: 在此表中,路径值不同,但可以具有相同的id\u位置 SELECT Name, Address, path from `table_location` as A inner JOIN table_images as B on A.id = B.id_location where A.des 这是一次重演 test1, example, pathExample1 test1, example, pat

我需要创建一个查询来返回多个值​,并将其作为结果列返回,例如:

在此表中,路径值不同,但可以具有相同的id\u位置

SELECT
    Name,
    Address,
    path
from
    `table_location` as A
inner JOIN table_images as B
on A.id = B.id_location

where A.des
这是一次重演

test1, example, pathExample1
test1, example, pathExample2
test3, example, pathExample3
test3, example, pathExample4
如何返回列中的路径

例如:

test1, example, pathExample1, pathExample2
test3, example, pathExample3, pathExample4

我是如何做到这一点的?

您可以使用
group\u concat()


最简单的方法是使用
group\u concat()

这会将结果放入一列中。如果需要两列,则可以使用:

select name, address, min(path), nullif(max(path), min(path))
from table_location l join
     table_images i
     on l.id = i.id_location
where A.des
group by name, address;

据我所知,问题是如何将每个位置的图像旋转到不同的列

如果预先知道每个位置的最大图像数,可以按如下方式进行条件聚合:

select l.name, l.address,
    max(case when i.rn = 1 then i.path end) path1,
    max(case when i.rn = 2 then i.path end) path2,
    max(case when i.rn = 3 then i.path end) path3
from table_location l
inner join (
    select i.*, row_number() over(partition by id_location order by path) rn
    from table_images i
) i on i.id_location_id = l.id
group by l.id
您可以在
select
子句中添加更多条件表达式,以处理每个位置的更多图像

请注意,
row\u number()
仅在MySQL 8.0中可用

select name, address, min(path), nullif(max(path), min(path))
from table_location l join
     table_images i
     on l.id = i.id_location
where A.des
group by name, address;
select l.name, l.address,
    max(case when i.rn = 1 then i.path end) path1,
    max(case when i.rn = 2 then i.path end) path2,
    max(case when i.rn = 3 then i.path end) path3
from table_location l
inner join (
    select i.*, row_number() over(partition by id_location order by path) rn
    from table_images i
) i on i.id_location_id = l.id
group by l.id