如何将数据从两个表保存到第三个表中。 [英] How do i save data from two tables into third table.
本文介绍了如何将数据从两个表保存到第三个表中。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这个SQL编码相对较新,一点帮助可能很有用。
问题:
如何从tabel1和table2中保存第三个表中的合并数据。
例如:
Table1 包含以下列和数据。
ID |名称|目标
11 | Abcd | 123
12 | xyzx | 135
表2 包含以下列和数据。
ID |候选人| Roll_Number
11 | asdeff | 12345
11 | dfrsf | 12346
11 | dfesf | 12347
12 | erfgv | 12348
12 | dsfdv | 12349
12 | dasfr | 12350
12 | dsadsa | 12351
表3 应存储来自table1&的数据。应用函数后的table2。
名称|候选人数|目标
Abcd | 3 | 123
xyzx | 4 | 135
注意:在第三个表中,候选人数列应分别从表2中计算。
我需要一个SQL语句,它在table3中生成以上数据。
谢谢
Saiekrishna
我尝试过:
我使用了JOIN,DISTINCT,COUNT但无法获得正确的数据。
解决方案
这听起来一个非常直接的解决方案
如下所示应该做你的工作 -
- - INSERT INTO Table3
SELECT Table1.Name,COUNT(Table2.ID ),表1. 目标
FROM 表2
INNER JOIN 表1 ON Table2.ID = Table1.ID
GROUP BY Table1.Name,Table1。目标
注意:一旦你进入,就取消注释INSERT语句确保SELECT语句的结果准确性,将结果插入Table3。
希望,它有帮助:)
Am relatively new to this SQL coding, a little help can be useful.
Question:
How do i save consolidated data in third table from tabel1 and table2.
For example:
Table1 contains following column and data.
ID | Name | Target
11 | Abcd | 123
12 | xyzx | 135
Table2 contains following columns and data.
ID | Candidate | Roll_Number
11 | asdeff | 12345
11 | dfrsf | 12346
11 | dfesf | 12347
12 | erfgv | 12348
12 | dsfdv | 12349
12 | dasfr | 12350
12 | dsadsa | 12351
Table3 should store data from table1 & table2 after applying functions.
Name | Number of Candidate | Target
Abcd | 3 | 123
xyzx | 4 | 135
Note: In the third table the "Number of candidate" column should be COUNTED from table2, respectively.
I need a SQL statements which generates above data in table3.
Thanks
Saiekrishna
What I have tried:
I have used JOIN, DISTINCT, COUNT but couldn't get correct data.
解决方案
This sounds to be a pretty much straight solution
Something like following should do your job-
--INSERT INTO Table3 SELECT Table1.Name, COUNT(Table2.ID),Table1.Target FROM Table2 INNER JOIN Table1 ON Table2.ID=Table1.ID GROUP BY Table1.Name,Table1.Target
Note: Uncomment the INSERT statement once you are sure about the accuracy of the result from the SELECT statement, to insert the result in to Table3.
Hope, it helps :)
这篇关于如何将数据从两个表保存到第三个表中。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文