[英]How can I join data from two tables and insert the result into a new table?
I have a table for my products and my key is Pid
.我的产品有一张桌子,我的钥匙是
Pid
。
Each product can have several types of modes that are registered in the table TB_Types
.每个产品都可以有几种类型的模式,这些模式在表
TB_Types
中注册。
did
is an identity column (key), pid
is foreign key to products table: did
是一个标识列(键), pid
是产品表的外键:
did![]() |
pid ![]() |
name![]() |
---|---|---|
1 ![]() |
1 ![]() |
type1![]() |
2 ![]() |
1 ![]() |
type2![]() |
3 ![]() |
2 ![]() |
type3![]() |
4 ![]() |
2 ![]() |
type4 ![]() |
Now we have a table where each product can have a different number table name: TB_Count
现在我们有一个表,其中每个产品可以有不同的数字表名:
TB_Count
Like the table below:如下表:
cid
is an identity column (key), pid
is a foreign key to the products table cid
是标识列(键), pid
是 products 表的外键
cid![]() |
pid ![]() |
count![]() |
---|---|---|
1 ![]() |
1 ![]() |
25 ![]() |
2 ![]() |
1 ![]() |
50 ![]() |
3 ![]() |
1 ![]() |
100 ![]() |
6 ![]() |
2 ![]() |
1000 ![]() |
7 ![]() |
2 ![]() |
5000 ![]() |
8 ![]() |
2 ![]() |
10000 ![]() |
Initially, the customer wanted each product to have different types and numbers, but now the count table must be changed as follows and the count that was for each product must be for each type.最初,客户希望每种产品具有不同的类型和编号,但现在计数表必须更改如下,并且每种产品的计数必须针对每种类型。
I created a table as it should be.我创建了一个应有的表格。
New table name: tb_newcount
新表名:
tb_newcount
id
is an identity column (key), did
is a foreign key to the TB_Types
table. id
是标识列(键), did
是TB_Types
表的外键。
id ![]() |
did![]() |
count![]() |
---|---|---|
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 ![]() |
If I can move the information to the new table as it was said, the problem will be solved.如果我可以按照所说的将信息移动到新表中,那么问题将得到解决。
I hope I have said what I mean.我希望我已经说出了我的意思。
Note: The number of products is close to 5,000 and the number of types is 2000 and the number of count is about 2000 records.注:产品数量接近5000种,种类数为2000种,计数约2000条记录。
thanks谢谢
You need INNER JOIN
as follows:您需要
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.