zoukankan      html  css  js  c++  java
  • 十一、一些有用的SQL查询语句

    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+"'

     

     

     

     

  • 相关阅读:
    Jmeter在命令行运行技巧
    Tomcat6 一些调优设置内存和连接数
    用例结构优化心得
    LoadRunner 调用dll方法
    DLL接口自动化测试总结
    Loadrunner进行md5加密方法
    Loadrunner检查点使用总结
    网络上可供测试的Web Service
    04 json,xml混合封装通信
    03 xml封装通信接口
  • 原文地址:https://www.cnblogs.com/liuyuanhao/p/3013432.html
Copyright © 2011-2022 走看看