本文實(shí)例講述了mysql實(shí)現(xiàn)查詢數(shù)據(jù)并根據(jù)條件更新到另一張表的方法。分享給大家供大家參考,具體如下:
原本的數(shù)據(jù)庫有3張表
- travel_way :旅游線路表,存放線路的具體信息
- traveltag :線路標(biāo)簽表,存放線路目的地等信息
- tagrelation:標(biāo)簽對(duì)應(yīng)表,存放線路和目的地的對(duì)應(yīng)關(guān)系
因?yàn)闃I(yè)務(wù)邏輯的改變,現(xiàn)在要把它們合并為一張表,把traveltag中的目的地信息插入到travel_way中。
首先獲取到所有線路對(duì)應(yīng)的目的地,以線路ID分組,合并目的地到一行,以逗號(hào)分隔。
復(fù)制代碼 代碼如下:
SELECT travel_way.id,GROUP_CONCAT(traveltag.content) FROM travel_way LEFT JOIN tagrelation on travel_way.id = tagrelation.travel_id LEFT JOIN traveltag ON tagrelation.tag_id = traveltag.id GROUP BY travel_way.id
先把查到的數(shù)據(jù)存放到了一個(gè)新建的表mid里
復(fù)制代碼 代碼如下:
INSERT into mid (travelway_id,destination) SELECT travel_way.id,GROUP_CONCAT(traveltag.content) FROM travel_way LEFT JOIN tagrelation on travel_way.id = tagrelation.travel_id LEFT JOIN traveltag ON tagrelation.tag_id = traveltag.id GROUP BY travel_way.id
然后將mid表的數(shù)據(jù)更新到travel_way里,因?yàn)槭歉?,所以不能用insert into select from 語句了
復(fù)制代碼 代碼如下:
update travel_way,mid set travel_way.destination = mid.destination where travel_way.id = mid.travelway_id
成功將目的地以逗號(hào)分隔的字符串形式導(dǎo)入travel_way表中
說一下用到的幾個(gè)方法,group_concat
group_concat( [DISTINCT] 要連接的字段 [Order BY 排序字段 ASC/DESC] [Separator '分隔符'] ),該函數(shù)能夠?qū)⑾嗤男薪M合起來
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字段的值在同一行打印出來,逗號(hào)分隔(默認(rèn))
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字段去重打印在一行,逗號(hào)分隔
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字段的值打印在一行,逗號(hào)分隔,按照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
將查詢到的記錄插入到某個(gè)表中,
INSERT INTO db1_name(field1,field2) SELECT field1,field2 FROM db2_name
要求目標(biāo)db2必須存在,下面測試一下,有兩個(gè)表,結(jié)構(gòu)如下
select * from insert_one;
+----+--------+-----+-----+
| id | name | age | sex |
+----+--------+-----+-----+
| 1 | 田小斯 | 25 | |
| 2 | 劉大牛 | 26 | |
| 3 | 鄭大錘 | 28 | |
| 4 | 胡二狗 | 30 | |
+----+--------+-----+-----+
4 rows in set
select * from insert_sex;
+----+-----+
| id | sex |
+----+-----+
| 1 | 1 |
| 2 | 2 |
| 3 | 1 |
| 4 | 2 |
+----+-----+
4 rows in set
從表2中查找性別數(shù)據(jù),插入到表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
結(jié)果很尷尬,我是想要更新這張表的sex字段,而不是插入新的數(shù)據(jù),那么這個(gè)命令只適用于要把數(shù)據(jù)導(dǎo)入空表中,所以在上面的實(shí)際需要中,我建立了新表mid,利用update來中轉(zhuǎn)并更新數(shù)據(jù)
復(fù)制代碼 代碼如下:
UPDATE tb1,tb2 SET tb1.address=tb2.address WHERE tb1.name=tb2.name
根據(jù)條件匹配,把表1的數(shù)據(jù)替換為(更新為)表2的數(shù)據(jù),表1和表2必須有關(guān)聯(lián)才可以
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 | 田小斯 | 25 | 1 |
| 2 | 劉大牛 | 26 | 2 |
| 3 | 鄭大錘 | 28 | 1 |
| 4 | 胡二狗 | 30 | 2 |
| 5 | | | 1 |
| 6 | | | 2 |
| 7 | | | 1 |
| 8 | | | 2 |
+----+--------+-----+-----+
8 rows in set
成功將數(shù)據(jù)更新到insert_one表的sex字段中。
更多關(guān)于MySQL相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《MySQL常用函數(shù)大匯總》、《MySQL日志操作技巧大全》、《MySQL事務(wù)操作技巧匯總》、《MySQL存儲(chǔ)過程技巧大全》及《MySQL數(shù)據(jù)庫鎖相關(guān)技巧匯總》
希望本文所述對(duì)大家MySQL數(shù)據(jù)庫計(jì)有所幫助。
您可能感興趣的文章:- Mysql update多表聯(lián)合更新的方法小結(jié)
- 如何使用MySQL一個(gè)表中的字段更新另一個(gè)表中字段
- Mysql 根據(jù)一個(gè)表數(shù)據(jù)更新另一個(gè)表的某些字段(sql語句)
- MySQL數(shù)據(jù)庫同時(shí)查詢更新同一張表的方法
- mysql用一個(gè)表更新另一個(gè)表的方法
- 僅用一句SQL更新整張表的漲跌幅、漲跌率的解決方案