標籤:style blog http color io ar 資料 sp div
今天遇到一個客戶的資料更新問題,兩個相關聯的表,一個主表用於儲存單據主要資訊,一個副表用於儲存單據的明細資訊;現在要把主表的其中一個欄位的資料更新到副表的一個欄位中儲存。
假設:A表是主表,有單號order_id、開單人operator、開單日期oper_date、備忘memo等;B表是副表,有單號order_id、序號id、商品編碼code、商品名稱name、備忘memo等。A表的備忘是有資料的,B表的備忘沒有資料,現在要把A表的資料更新到B表,並且B表有資料的不能更新了。A表與B表是以單號來關聯的。更新資料的SQL文法如下:
update B,A set B.memo=A.memo
where A.order_id=B.order_id and (B.memo is null or B.memo=‘‘);
create table A( order_id int not null auto_increment, operator varchar(50), oper_date date, memo varchar(50), primary key(order_id));create table B( order_id int not null auto_increment, good_id int, good_code int, good_name varchar(50), memo varchar(50), primary key (order_id));insert into A values (1,‘onion2‘,now(),‘測試1‘),(2,‘onion2‘,now(),‘測試2‘),(3,‘onion3‘,now(),‘測試3‘);insert into B values(1,1,0001,‘good1‘,‘已經備忘‘);insert into B (order_id,good_id,good_code,good_name) values (2,2,0002,‘good2‘),(3,3,0003,‘good3‘);update B,A set B.memo=A.memowhere A.order_id=B.order_id and (B.memo is null or B.memo=‘‘);
mysql 兩個關聯表如何更新其中一個表的資料