【问题标题】:Combine Two SQL queries in one statement [duplicate]在一个语句中组合两个 SQL 查询[重复]
【发布时间】:2023-03-21 12:05:01
【问题描述】:

如何将两个 SQl 查询合并为一个

在我的例子中是 3 个表名

Remain_cotton,add_cotton,Sell_Table

 conn.Execute _
    "INSERT INTO Remain_Cotton(No_Of_Bottle) " & _
    "SELECT sum(No_Of_Bottle)" & _
    "FROM add_cotton Where Cateogry='Large'"

   conn.Execute _
    "INSERT INTO Remain_Cotton(Quantity) " & _
    "SELECT sum(Quantity)" & _
    "FROM Sell_Detail Where Cateogry='Large'"

我想要什么

     conn.Execute _
    "INSERT INTO Remain_Cotton(No_Of_Bottle)(Quantity) " & _
    "SELECT sum(No_OF_Bottle),sum(Quantity)" & _
    "FROM add_cotton,Sell_Detail Where Cateogry='Large'"

我之前问过类似的问题,但它是一个SQL语句,所以请不要将它与上一个连接起来

【问题讨论】:

  • 如果您发布您的实际代码、相关的数据库架构和您看到的错误消息会有所帮助
  • **Dim large_tbl As String Dim sell_large As String **large_tbl = "SELECT Sum(No_Of_Bottle) FROM add_cotton where Cateogry='Large'" sell_large = "SELECT Sum(Quantity) FROM Sell_Detail where Cateogry= '大'"
  • 如果你编辑你的问题会更好(这次是为你做的) - 你仍然没有显示任何运行查询的代码或任何试图处理结果的代码,你没有解释什么您看到的错误/结果
  • 非常感谢我编辑了它等等
  • @Deanna 对不起??请您详细说明一下....

标签: sql database


【解决方案1】:

如果您使用的是 SQL 服务器,则可以使用 EXCEPT

Select Sum() From Table 1 Where Table=....** 
EXCEPT
Select Sum() From Table 2 Where Table 2=........**  

【讨论】:

  • 谢谢,但是 Visual basic 可以减去两个查询结果吗???
  • 使用 LINQ EXCEPT 所以它类似于query3 = query1.Except(query2) 假设您将数据放入 VB 中的任何内容都是 IEnumerable。你不能在数据库级别获得正确的数据集吗?
猜你喜欢
  • 1970-01-01
  • 2011-06-06
  • 2015-06-25
  • 2015-11-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-20
相关资源
最近更新 更多