mysql 兩個關聯表如何更新其中一個表的資料

來源:互聯網
上載者:User

標籤: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 兩個關聯表如何更新其中一個表的資料

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.