前言
最近需要寫聯表多字段update更新語句,發現不同的數據庫,SQL語法也不一樣,正好我這里有MySQL、Oracle、PgSQL三種數據庫環境,分別練習、實操這三種數據庫的聯表update語句
本文記錄MySQL、Oracle、PgSQL聯表多字段update更新語句實操練習過程
練習需求
一張user用戶表、一張blog博客表,由於不同的數據庫,sql語法不用,這里給出mysql的表數據語句,其他數據庫類型自行轉換
-- user用戶表 CREATE TABLE `user` ( `user_id` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '用戶id', `user_name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '用戶名稱', PRIMARY KEY (`user_id`) USING BTREE ) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci COMMENT = '用戶表' ROW_FORMAT = Compact; INSERT INTO `user` VALUES ('1', '張三'); INSERT INTO `user` VALUES ('2', '李四'); INSERT INTO `user` VALUES ('3', '王五');
-- blog博客表 CREATE TABLE `blog` ( `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '博客id', `title` varchar(200) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '博客標題', `content` mediumtext CHARACTER SET utf8 COLLATE utf8_general_ci NULL COMMENT '博客內容', `user_id` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '用戶id', PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB AUTO_INCREMENT = 17 CHARACTER SET = utf8 COLLATE = utf8_general_ci COMMENT = '博客表' ROW_FORMAT = Compact; INSERT INTO `blog` VALUES (2, 'test 1', 'test 1', '1'); INSERT INTO `blog` VALUES (3, 'test 2', 'test 2', '1'); INSERT INTO `blog` VALUES (4, 'test 4', 'test 4', '2'); INSERT INTO `blog` VALUES (5, 'test 5', 'test 5', '2'); INSERT INTO `blog` VALUES (6, 'test 6', 'test 6', '1'); INSERT INTO `blog` VALUES (11, '11', '11', '3'); INSERT INTO `blog` VALUES (12, '12', '12', '3'); INSERT INTO `blog` VALUES (13, '13', '13', '3'); INSERT INTO `blog` VALUES (14, '14', '14', '3'); INSERT INTO `blog` VALUES (15, '15', '15', '3'); INSERT INTO `blog` VALUES (16, '16', '16', '3');
練習需求:更新每個用戶的博客id最大的博客內容,新內容為:用戶名稱user_name + 博客內容content
需要修改的id,以及新內容
SQL
mysql
select * from blog select * from `user` -- 查出虛表table1 -- 練習需求:更新每個用戶的博客id最大的博客內容,新內容為:用戶名稱user_name + 博客內容content SELECT t.id, concat( t.user_name, b.content ) content FROM blog b JOIN ( SELECT u.user_id, u.user_name, max( b.id ) AS id FROM blog b JOIN `user` u ON u.user_id = b.user_id GROUP BY u.user_id, u.user_name ) t ON b.id = t.id -- MySQL聯表多字段update更新語句 update blog b ,(table1) t set b.content = t.content where b.id = t.id
為了方便閱讀避免貼出一堆長長的sql,此時update語句並不完整,執行時需要將table1虛表的查詢sql,替換到下面的update語句中table1,再執行update語句
oracle
select * from "blog" select * from "user" -- 查出虛表table1 -- 練習需求:更新每個用戶的博客id最大的博客內容,新內容為:用戶名稱user_name + 博客內容content SELECT t."id", concat( t."user_name", b."content" ) AS "content" FROM "blog" b JOIN ( SELECT u."user_id", u."user_name", max( b."id" ) AS "id" FROM "blog" b JOIN "user" u ON u."user_id" = b."user_id" GROUP BY u."user_id", u."user_name" ) t ON b."id" = t."id" -- Oracle聯表多字段update更新語句 update "blog" b set (b."content") = (select t."content" from (table1) t where b."id" = t."id") where exists (select 1 from (table1) t where b."id" = t."id")
為了方便閱讀避免貼出一堆長長的sql,此時update語句並不完整,執行時需要將table1虛表的查詢sql,替換到下面的update語句中table1,再執行update語句
pgsql
select * from "blog" select * from "user" -- 查出虛表table1 -- 練習需求:更新每個用戶的博客id最大的博客內容,新內容為:用戶名稱user_name + 博客內容content SELECT t."id", concat( t."user_name", b."content" ) AS "content" FROM "blog" b JOIN ( SELECT u."user_id", u."user_name", max( b."id" ) AS "id" FROM "blog" b JOIN "user" u ON u."user_id" = b."user_id" GROUP BY u."user_id", u."user_name" ) t ON b."id" = t."id" -- PgSQL聯表多字段update更新語句 update "blog" b set b."content" = t."content" from (table1) t where b."id" = t."id"
為了方便閱讀避免貼出一堆長長的sql,此時update語句並不完整,執行時需要將table1虛表的查詢sql,替換到下面的update語句中table1,再執行update語句
練習效果
以上三種數據庫類型,SQL執行結果均為
上大招
實在不行了,可以直接拼接出update語句,再把update語句復制出來執行!
我們以mysql為例(PS:由於單引號',是特殊字符,拼接時我們用$代替,后面再進行全部替換即可)
-- 查看、對比新舊數據 SELECT t.id, b.content, concat( t.user_name, b.content ) new_content -- 備份原數據 SELECT concat( concat( 'update blog set content = $', b.content ), concat( '$ where id = $', concat( t.id, '$;' )) ) AS str -- update語句 SELECT concat( concat( 'update blog set content = $', concat( t.user_name, b.content ) ), concat( '$ where id = $', concat( t.id, '$;' )) ) AS str FROM blog b JOIN ( SELECT u.user_id, u.user_name, max( b.id ) AS id FROM blog b JOIN `user` u ON u.user_id = b.user_id GROUP BY u.user_id, u.user_name ) t ON b.id = t.id
查看、對比新舊數據
備份原數據
update語句
這時候就可以愉快的執行update語句了
后記
PS:執行update語句一定要帶上where條件,否則一不小心就變成更新全表
聯表多字段update更新語句暫時先記錄到這,后續再進行補充