简体   繁体   English

如何连接两个表中的数据并将结果插入到新表中?

[英]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 PID name姓名
1 1 1 1 type1类型1
2 2 1 1 type2类型2
3 3 2 2 type3类型3
4 4 2 2 type4 4型

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 PID count数数
1 1 1 1 25 25
2 2 1 1 50 50
3 3 1 1 100 100
6 6 2 2 1000 1000
7 7 2 2 5000 5000
8 8 2 2 10000 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是标识列(键), didTB_Types表的外键。

id ID did做过 count数数
1 1 1 1 25 25
2 2 1 1 50 50
3 3 1 1 100 100
4 4 2 2 25 25
5 5 2 2 50 50
6 6 2 2 100 100
7 7 3 3 5000 5000
8 8 3 3 10000 10000
9 9 3 3 15000 15000
10 10 4 4 5000 5000
11 11 4 4 10000 10000
12 12 4 4 15000 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.

相关问题 如何使用内部联接从两个表中 select 数据并直接插入第三个表? - How can I select data from two tables using an inner join and insert directly into a third table? 如何将存储过程的结果插入到新表中? - How can I insert result from stored procedure to a new table? 如何将两个表中的数据插入到一个表中? - How can I insert data from two tables into one? 我可以在MYSQL中联接两个表并创建一个新表吗 - Can I join two tables in MYSQL and create a new table 我怎样才能将这些数据从其他表中插入到这个表中? - how can i insert this data into this table from other tables? 如何连接两个表,但从表1中获得结果,而表2中没有条目? - How can I join two tables but get results from table one where theres no entry in table two? 如何将这两个表连接到同一个Sql结果中 - How can I join these two tables into the same Sql result 如何连接两个表(均源自自连接)以创建第三个表? - How can I join two tables( both derived from self join) to create a third table? 如何从其他两个表填充表中的数据 - How can I fill data in table from two other tables 你能加入两个表并得到一个包含 obj 列表的 obj(来自第一个表)(来自第二个表) - can you join two tables and result with an obj (from first table) containing a list of obj(from the second table)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM