如何连接 MySQL 中的字段?
要连接MySQL中的字段,您可以GROUP_CONCAT()与GROUPBY一起使用。让我们首先创建一个表-
mysql> create table DemoTable ( StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY, StudentName varchar(30), StudentScore int );
使用插入命令在表中插入记录-
mysql> insert into DemoTable( StudentName,StudentScore) values('Bob',80);
mysql> insert into DemoTable( StudentName,StudentScore) values('Bob',80);
mysql> insert into DemoTable( StudentName,StudentScore) values('Chris',90);
mysql> insert into DemoTable( StudentName,StudentScore) values('Chris',70);
mysql> insert into DemoTable( StudentName,StudentScore) values('Bob',50);
mysql> insert into DemoTable( StudentName,StudentScore) values('David',60);
mysql> insert into DemoTable( StudentName,StudentScore) values('Chris',99);
mysql> insert into DemoTable( StudentName,StudentScore) values('David',88);使用select语句显示表中的所有记录-
mysql> select * from DemoTable;
这将产生以下输出-
+-----------+-------------+--------------+ | StudentId | StudentName | StudentScore | +-----------+-------------+--------------+ | 1 | Bob | 80 | | 2 | Bob | 80 | | 3 | Chris | 90 | | 4 | Chris | 70 | | 5 | Bob | 50 | | 6 | David | 60 | | 7 | Chris | 99 | | 8 | David | 88 | +-----------+-------------+--------------+ 8 rows in set (0.00 sec)
以下是连接MySQL中的字段的查询-
mysql> select StudentName, group_concat(StudentScore separator ',') as Score from DemoTable group by StudentName;
这将产生以下输出-
+-------------+----------+ | StudentName | Score | +-------------+----------+ | Bob | 80,80,50 | | Chris | 90,70,99 | | David | 60,88 | +-------------+----------+ 3 rows in set (0.24 sec)
热门推荐
10 文艺结婚祝福语短语简短
11 婚礼结束聚餐祝福语简短
12 周年庆典祝福语致辞简短
13 直播祝福语老师文案简短
14 幼教专干调动祝福语简短
15 九子订婚祝福语简短
16 端午发给客户祝福语简短
17 许愿星姐妹祝福语简短
18 蛋糕祝老师祝福语简短