1、 将一个表的一些字段插入到另一个表中。
Insert Into 表1(表1字段1,表1字段2) Selecet 表2字段1,表2字段2 from 表2
INSERT INTO Tongji
(TongjiMajorID, TongjiMajorSumScore, TongjiMajorPerson)
SELECT ReviewMajorID, AVG(ReviewSumScore) AS Expr1, COUNT(*) AS Expr2
FROM tbReview
GROUP BY ReviewMajorID
2、 在一个表增加一个另一个表的字段
第一种方法:update 表1 set 表1的字段=表2的字段 from 表1,表2 where 表1的字段=表2的字段
update Tongji set TongjiMajorName =MajorName,TongjiDepartmentID=MajorDepartmentID from Tongji,tbMajor where TongjiMajorID=MajorID
update Tongji set TongjiDepartmentName =DepartmentName from Tongji,tbDepartment where TongjiDepartmentID=DepartmentID
第二种方法:update 表1 set表1的字段=表2的字段 from 表1 INNER JOIN 表1 on 表1字段1=表2字段1
UPDATE Tongji
SET TongjiDepartmentName = tbDepartment.DepartmentName
FROM Tongji INNER JOIN
tbDepartment ON Tongji.TongjiDepartmentID = tbDepartment.DepartmentID CROSS JOIN
Tongji AS Tongji_1
最近做了个小项目,留在这里以后备查。
最后的统计用的查询,当然以也可以写成存储过程:
正确的查询1:
INSERT INTO tbMerger
(MergerMajorID, MergerMajorName, MergerDepartmentID)
SELECT MajorID, MajorName, MajorDepartmentID
FROM tbMajor
2:
UPDATE tbMerger
SET MergerReviewSumScore = Tongji.TongjiMajorSumScore, MergerReviewPerson = Tongji.TongjiMajorPerson
FROM Tongji INNER JOIN
tbMerger ON Tongji.TongjiMajorID = tbMerger.MergerMajorID
3:
UPDATE tbMerger
SET MergerJwcSumScore = tbJwcReview.JwcReviewSumScore
FROM tbJwcReview INNER JOIN
tbMerger ON tbJwcReview.JwcReviewMajorID = tbMerger.MergerMajorID
4、UPDATE tbMerger
SET MergerDepartmentName = tbDepartment.DepartmentName
FROM tbDepartment INNER JOIN
tbMerger ON tbDepartment.DepartmentID = tbMerger.MergerDepartmentID
5、UPDATE tbMerger
SET MergerTotalMajorScore = MergerReviewSumScore + MergerJwcSumScore
6、update tbMerger set MergerDateTime='"+DateTime.Now+"'