mysql中update和select結(jié)合使用方式
mysql update和select結(jié)合使用
在遇到需要update設(shè)置的參數(shù)來自從其他表select出的結(jié)果時,需要把update和select結(jié)合使用,不同數(shù)據(jù)庫支持的形式不一樣
在mysql中如下:
update A inner join(select id,name from B) c on A.id = c.id set A.name = c.name;
根據(jù)AB兩個表的id相同為條件,把A表的name修改為B的sql語句就如上所示
sql批量更新update嵌套select更新
概述
有兩張表【user】和【city】,user表的 city_uuid 、 city_no 和 city 表的 city_uuid 、 city_no 一一對應(yīng),但是 user 表只有 city_uuid ,這時候需要將 city 對應(yīng)的 city_no 批量更新到 user 表中


批量更新方式
第一種方式(inner join 內(nèi)連接)
update u set u.city_no = c.city_no from user u inner join city c on u.city_uuid = c.city_uuid where u.city_uuid is not null and u.city_no is null
第二種方式(子查詢)
update u set u.city_no = (select c.city_no from city c where u.city_uuid = c.city_uuid) from user u
第三種方式:(笛卡爾積)
update u set u.city_no = c.city_no from [user] u, city c where u.city_uuid = c.city_uuid
update 多表更新
update table1 t1,table2 t2, table3 t3, ... , tablen tn set t1.column= ?, t2.column, t3.column = ?, ... , tn.column = ? where t1.xx= ?, t2.xx = ?, ... , tn.xx = ?
案例:(conditionUuid是user表的外鍵,每個conditionUuid對應(yīng)兩條user記錄,將producter記錄覆蓋consumer記錄的指定字段值)
update r2 set r2.userUuid = r1.userUuid, r2.userName = r1.userName , r2.age = r1.age, r2.updatedTime = '2021-02-22 22:22:22.222' from user r1 inner join user r2 on r1.conditionUuid = r2.conditionUuid where r1.conditionValue = 'condition-consumer-00000000000000000' and r1.userName is not null and r2.conditionValue = 'condition-producter-0000000000000000' and r2.userName is not null
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
關(guān)于skip_name_resolve參數(shù)的總結(jié)分享
下面小編就為大家?guī)硪黄P(guān)于skip_name_resolve參數(shù)的總結(jié)分享。小編覺得挺不錯的,現(xiàn)在分享給大家。給大家一個參考。一起跟隨小編過來看看吧2016-03-03
本文我們主要介紹了MySQL性能分析以及explain的使用,包括:組合索引、慢查詢分析、MYISAM和INNODB的鎖定、MYSQL的事務(wù)配置項等,希望能夠?qū)δ兴鶐椭?/div> 2011-08-08
MySQL進(jìn)行大數(shù)據(jù)量分頁的優(yōu)化技巧分享
mysql大數(shù)據(jù)量分頁情況下性能會很差,所以本文就來講一講mysql大數(shù)據(jù)量下偏移量很大,性能很差的問題,并附上解決方式,希望對大家有所幫助2024-01-01
解析SQL 表結(jié)構(gòu)信息查詢 含主外鍵、自增長
本篇文章是對SQL 表結(jié)構(gòu)信息查詢 含主外鍵、自增長進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
mysql8.0.11安裝配置方法圖文教程 MySQL8.0新密碼認(rèn)證方式
這篇文章主要為大家詳細(xì)介紹了mysql8.0.11安裝配置方法圖文教程,以及MySQL8.0新密碼認(rèn)證方式,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-11-11
一文弄懂MySQL中redo?log與binlog的區(qū)別
在學(xué)習(xí)mysql數(shù)據(jù)庫時,不可避免要去接觸到redo log和binlog,好多人對這兩者的概念分不太清,下面這篇文章主要給大家介紹了關(guān)于MySQL中redo?log與binlog區(qū)別的相關(guān)資料,需要的朋友可以參考下2022-02-02最新評論

