mysql5 7 多表字段更新

2021-08-04 17:53:09 字數 2394 閱讀 9275

用乙個表中的字段去更新另外乙個表中的字段, mysql 中有相應的 update 語句來支援,不過這個 update 語法有些特殊。看乙個例子就明白了。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

create table student

(

student_idintnotnull

,student_name  varchar(30)  notnull

,city_code     varchar(10)null

,city_name     varchar(50)null

);

create table city

(

code varchar(10) notnull

,name varchar(50) notnull

);

insert into student values(1,'john','001',null);

insert into student values(2,'nick','002',null);

insert into city values('001','beijing');

insert into city values('002','shanghai');

insert into city values('003','shenzhen');

有兩個表:student & city,現在需要取出 city.name 來更新 student.city_name。兩表關聯條件是 student.city_code=city.code。

update student s, city c

set s.city_name = c.name

where s.city_code = c.code;

也可以試下面的相關子查詢:

update student s set city_name = (select name from city where code = s.city_code);

mysql5 7學習 mysql 5 7 學習

mysql uroot proot mysql5.7 mysql.user表沒有password欄位改 authentication string 一.建立使用者 命令 create user username host identified by password 例子 create user d...

mysql5 7如何開啟 mysql57怎麼開啟

開啟mysql57的方法 首先開啟winodws執行視窗 然後在開啟編輯框中輸入cmd命令 最後在終端介面中輸入 mysql hlocalhost uroot p123 即可顯示開啟mysql資料庫。windows下用命令列啟動mysql5.7 win菜單鍵即是在鍵盤左下角 ctrl控制 鍵與 al...

mysql5 7如何開啟 mysql57怎麼開啟

開啟mysql57的方法 首先開啟winodws執行視窗 然後在開啟編輯框中輸入cmd命令 最後在終端介面中輸入 mysql hlocalhost uroot p123 即可顯示開啟mysql資料庫。windows下用命令列啟動mysql5.7 win菜單鍵即是在鍵盤左下角 ctrl控制 鍵與 al...