[英]How can I select data from two tables using an inner join and insert directly into a third table?
[英]How can I join data from two tables and insert the result into a new table?
我的產品有一張桌子,我的鑰匙是Pid
。
每個產品都可以有幾種類型的模式,這些模式在表TB_Types
中注冊。
did
是一個標識列(鍵), pid
是產品表的外鍵:
做過 | PID | 姓名 |
---|---|---|
1 | 1 | 類型1 |
2 | 1 | 類型2 |
3 | 2 | 類型3 |
4 | 2 | 4型 |
現在我們有一個表,其中每個產品可以有不同的數字表名: TB_Count
如下表:
cid
是標識列(鍵), pid
是 products 表的外鍵
西德 | PID | 數數 |
---|---|---|
1 | 1 | 25 |
2 | 1 | 50 |
3 | 1 | 100 |
6 | 2 | 1000 |
7 | 2 | 5000 |
8 | 2 | 10000 |
最初,客戶希望每種產品具有不同的類型和編號,但現在計數表必須更改如下,並且每種產品的計數必須針對每種類型。
我創建了一個應有的表格。
新表名: tb_newcount
id
是標識列(鍵), did
是TB_Types
表的外鍵。
ID | 做過 | 數數 |
---|---|---|
1 | 1 | 25 |
2 | 1 | 50 |
3 | 1 | 100 |
4 | 2 | 25 |
5 | 2 | 50 |
6 | 2 | 100 |
7 | 3 | 5000 |
8 | 3 | 10000 |
9 | 3 | 15000 |
10 | 4 | 5000 |
11 | 4 | 10000 |
12 | 4 | 15000 |
如果我可以按照所說的將信息移動到新表中,那么問題將得到解決。
我希望我已經說出了我的意思。
注:產品數量接近5000種,種類數為2000種,計數約2000條記錄。
謝謝
您需要INNER JOIN
如下:
INSERT INTO tb_newcount (did, count)
select did, count
from TB_Types t join TB_Count c on c.pid = t.pid
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.