如何在MySQL中根据对应的重复ID值连接列?在同一列中显示重复值,并用斜杠分隔
为此,您可以使用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)
热门推荐
1 狗狗技能训练
5 如何训练狗狗笑
6 狗狗训练排便
9 狗狗响片训练
10 如何训练狗狗不捡食
11 狗狗尿训练
12 怎么训练狗狗找东西
13 狗狗训练站立
14 狗狗训练飞盘
15 鞍山狗狗训练
16 怎样训练泰迪狗狗谢谢
17 狗狗外出训练
18 怎么训练金毛狗狗大小便