sql語句,不知如何寫

來源:互聯網
上載者:User
sql語句求助,不知怎麼寫。
我現在有兩張表 A表 和 B表 A表有個欄位是zhanghao,mobile, B表有zhanghao,username,address, 現在A表總共有15條資料,B表有20條資料,我想把A表和B表相同欄位值相等插入到第三張表裡,把多餘的那五條資料也插入到第三張表裡,也就是相等的插入到一條資料,不相等的重新插入到一條資料,怎麼弄?謝謝大家了。

------解決方案--------------------
用A、B表關聯 帳號相等的插入 C表 不相等的插入D 表
SQL code
INSERT INTO C (    欄位1,    欄位2,    欄位3    )    欄位1,    欄位2,    欄位3    FROM A , B     where A.zhanghao = B.zhanghao;        INSERT INTO D (    欄位1,    欄位2,    欄位3    )    欄位1,    欄位2,    欄位3    FROM A , B     where A.zhanghao != B.zhanghao;
------解決方案--------------------
insert into C select * from A ,B where A.zhanghao=B.zhanghao;

insert into C select * from B where zhanghao not in(select zhanghao from A);
------解決方案--------------------
我意思如果A表和B表相同欄位的值相等的插入一條資料到第三張表,如果不相等只把A表資料插入到第三張表裡就行了。

你這要求我覺得彆扭。
A
1 mobile1
2 mobile2
3 mobile3
B
1 zhang bj
2 li shang
4 wang ji

是不是zhanghao一樣的,將A+B資料插入C ,而A中存在B不存在的只把A插入C???
------解決方案--------------------
還有 A 有 B 沒有的呢?

insert into C
select * from A, B where A.zhanghao=B.zhanghao
union
select * from A
union
select * from B



------解決方案--------------------
所以啊 問問題的人不把問題描述清楚 還要我們這些回答的人給他弄出來。
哎~~~~~
------解決方案--------------------
探討

還有 A 有 B 沒有的呢?

insert into C
select * from A, B where A.zhanghao=B.zhanghao
union
select * from A
union
select * from B
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.