数据案例
原本的数据库有3张表。
- t_user :用户表,存放用户的基本信息。
- t_role :角色表,存放角色信息。
- t_role_user:存放角色与用户的对应关系。
因为业务逻辑的改变,现在要把它们合并为一张表,把t_role中的角色信息插入到t_user中。
首先获取到所有用户对应的角色,以用户ID分组,合并角色地到一行,以逗号分隔。
SELECT t_user.id,GROUP_CONCAT(t_role.content) FROM t_user LEFT JOIN t_role_user on t_user.id = t_role_user.t_user_id LEFT JOIN t_role ON t_role_user.t_role_id = t_role.id GROUP BY t_user.id
先把查到的数据存放到了一个新建的表mid里
INSERT into mid (t_user_id,t_role_info) SELECT t_user.id,GROUP_CONCAT(t_role.info) FROM t_user LEFT JOIN t_role_user on t_user.id = t_role_user.t_user_id LEFT JOIN t_role ON t_role_user.t_role_id = t_role.id GROUP BY t_user.id
然后将mid表的数据更新到t_user里,因为是更新,所以不能用insert into select from 语句了
update t_user,mid set t_user.t_role_info = mid.t_role_info where t_user.id = mid.t_user_id
成功将目的地以逗号分隔的字符串形式导入t_user表中
说一下用到的几个方法,group_concat
group_concat( [DISTINCT] 要连接的字段 [Order BY 排序字段 ASC/DESC] [Separator '分隔符'] ),该函数能够将相同的行组合起来
select * from goods;
+------+------+
| id| price|
+------+------+
|1 | 10|
|1 | 20|
|1 | 20|
|2 | 20|
|3 | 200 |
|3 | 500 |
+------+------+
6 rows in set (0.00 sec)
以id分组,把price字段的值在同一行打印出来,逗号分隔(默认)
select id, group_concat(price) from goods group by id;
+------+--------------------+
| id| group_concat(price) |
+------+--------------------+
|1 | 10,20,20|
|2 | 20 |
|3 | 200,500|
+------+--------------------+
3 rows in set (0.00 sec)
以id分组,把price字段去重打印在一行,逗号分隔
select id,group_concat(distinct price) from goods group by id;
+------+-----------------------------+
| id| group_concat(distinct price) |
+------+-----------------------------+
|1 | 10,20|
|2 | 20 |
|3 | 200,500 |
+------+-----------------------------+
3 rows in set (0.00 sec)
以id分组,把price字段的值打印在一行,逗号分隔,按照price倒序排列
select id,group_concat(price order by price desc) from goods group by id;
+------+---------------------------------------+
| id| group_concat(price order by price desc) |
+------+---------------------------------------+
|1 | 20,20,10 |
|2 | 20|
|3 | 500,200|
+------+---------------------------------------+
3 rows in set (0.00 sec)
insert into select from
将查询到的记录插入到某个表中
INSERT INTO db1_name(field1,field2) SELECT field1,field2 FROM db2_name
要求目标db2必须存在,下面测试一下,有两个表,结构如下
select * from insert_one;
+----+--------+-----+-----+
| id | name | age | sex |
+----+--------+-----+-----+
| 1 | 冰河001 | 25 | |
| 2 | 冰河002 | 26 | |
| 3 | 冰河003 | 28 | |
| 4 | 冰河004 | 30 | |
+----+--------+-----+-----+
4 rows in set
select * from insert_sex;
+----+-----+
| id | sex |
+----+-----+
| 1 | 1 |
| 2 | 2 |
| 3 | 1 |
| 4 | 2 |
+----+-----+
4 rows in set
从表2中查找性别数据,插入到表1中
into insert_one(sex) select sex from insert_sex;
Query OK, 4 rows affected
select * from insert_one;
+----+--------+-----+-----+
| id | name | age | sex |
+----+--------+-----+-----+
| 1 | 田小斯 | 25 | |
| 2 | 刘大牛 | 26 | |
| 3 | 郑大锤 | 28 | |
| 4 | 胡二狗 | 30 | |
| 5 | | | 1 |
| 6 | | | 2 |
| 7 | | | 1 |
| 8 | | | 2 |
+----+--------+-----+-----+
8 rows in set
结果很尴尬,我是想要更新这张表的sex字段,而不是插入新的数据,那么这个命令只适用于要把数据导入空表中,所以在上面的实际需要中,我建立了新表mid,利用update来中转并更新数据
UPDATE tb1,tb2 SET tb1.address=tb2.address WHERE tb1.name=tb2.name
根据条件匹配,把表1的数据替换为(更新为)表2的数据,表1和表2必须有关联才可以
update insert_one,insert_sex set insert_one.sex = insert_sex.sex where insert_one.id = insert_sex.id;
Query OK, 4 rows affected
select * from insert_one;
+----+--------+-----+-----+
| id | name | age | sex |
+----+--------+-----+-----+
| 1 | 冰河001 | 25 | 1 |
| 2 | 冰河002 | 26 | 2 |
| 3 | 冰河003 | 28 | 1 |
| 4 | 冰河004 | 30 | 2 |
| 5 | | | 1 |
| 6 | | | 2 |
| 7 | | | 1 |
| 8 | | | 2 |
+----+--------+-----+-----+
8 rows in set
成功将数据更新到insert_one表的sex字段中。