为此,您可以使用GROUP_CONCAT()。
让我们首先创建一个表-
create table DemoTable764 ( ProductId int, ProductPrice int );
使用插入命令在表中插入一些记录-
insert into DemoTable764 values(101,10000); insert into DemoTable764 values(102,1090); insert into DemoTable764 values(103,4000); insert into DemoTable764 values(102,3450); insert into DemoTable764 values(101,20000); insert into DemoTable764 values(104,50000);
使用select语句显示表中的所有记录-
select *from DemoTable764;
这将产生以下输出-
+-----------+--------------+ | ProductId | ProductPrice | +-----------+--------------+ | 101 | 10000 | | 102 | 1090 | | 103 | 4000 | | 102 | 3450 | | 101 | 20000 | | 104 | 50000 | +-----------+--------------+ 6 rows in set (0.00 sec)
以下是将具有相应重复ID值的列连接起来的查询-
select ProductId, group_concat(ProductPrice SEPARATOR '/') AS ProductPrice from DemoTable764 group by ProductId;
这将产生以下输出-
+-----------+---------------+ | ProductId | ProductPrice | +-----------+---------------+ | 101 | 10000/20000 | | 102 | 1090/3450 | | 103 | 4000 | | 104 | 50000 | +-----------+---------------+ 4 rows in set (0.00 sec)