大家好,新手学习遇到如下问题向各位高手求助,
有两个表
表a  名称      数量      表b  名称     数量
    SIM模块    100           避雷器     100
    避雷器     100           雨量计      50 
     电源      200           电池板     100 表b作为入库表,表a做为库存表。 想将表b中的内容加到表中:名称相同的数量加到一起,不同的插入后一块显示。要在DBGrid中显示的 内容如下:
 
  名称    数量SIM模块   100
避雷器    200
电源      200
雨量计    50
电池板    100我尝试在dataset的CommandTex中输入以下SQl语句select a.名称,a.数量+b.数量 as 数量
from a,b where a.名称=b.名称
union
select a.名称,a.数量 from a,b where a.名称<>b.名称这样的语句执行后在Dbgrid中显示的结果:
  名称    数量
SIM模块   100 
避雷器    100
避雷器    200
电源      200我也知道选择是的a表中的字段值 但union 连接的是前后的字段类型必须相同
也尝试了插入子查询 但就是不能得到预想的结果 。。十分苦闷
请各大侠指点 。。谢谢

解决方案 »

  1.   

    Select 名称, 
           数量+
           (Case When (Select Sum(数量) From 表B Where 名称 = 表A.名称) Is Null Then 0 
                 Else (Select Sum(数量) From 表B Where 名称 = 表A.名称) End) 数量
    From 表AUnionSelect 名称, 数量 From 表B Where 名称 Not In (Select Distinct 名称 From 表A)Group By 名称
      

  2.   

    对不起, 笔误:
    Select 名称, 
           数量+
           (Case When (Select Sum(数量) From 表B Where 名称 = 表A.名称) Is Null Then 0 
                 Else (Select Sum(数量) From 表B Where 名称 = 表A.名称) End) 数量
    From 表AUnionSelect 名称, Sum(数量) From 表B Where 名称 Not In (Select Distinct 名称 From 表A)Group By 名称
      

  3.   

    非常感谢阿 
    可是当按照你的语句输入到ADODataset中的CommandText 中去之后 执行 令ADODataset.active:=true 之后 提示 无法指定的错误 不知为什么/是不是将
    Select 名称, 
           数量+
           (Case When (Select Sum(数量) From 表B Where 名称 = 表A.名称) Is Null Then 0 
                 Else (Select Sum(数量) From 表B Where 名称 = 表A.名称) End) 数量
    最后的 数量 之前 加上个 as 呢?。。
      

  4.   

    Select 名称, Sum(数量) 数量 //我漏了数量2个字From 表B Where 名称 Not In (Select Distinct 名称 From 表A)Group By 名称
      

  5.   

    To: 老冯 ,高手 十分感谢啊。看明白了,但还得请教您,下面是我在ADODataset中输入的完整的sql语句,完全是按照你的思路写的,就是换了一下字母名称,StorIn是表B,StoreAll是表a,select PoductName,Product_Num+(Case when (Select sum(Ptoduct_Num)  From StorIn Where ProductName=StoreAll.ProdutName) is null then 0 else (select sum(Product_Num) from StorIn where 
    ProductName=StoreAll.Name) end  Product_Num
    from StoreAll
    Union
     Selec ProductName, sum(Product_Num) Product_Num
    from StorIn
    where ProductName not in (select distinct Product_Name from StoreAll)
    Group by ProductName令ADODataset的属性Active:= true后 显示的是: 未指定错误很郁闷 。。我看到您的思路是没有问题的,但为什么会出现这样的提示,难道是Delphi中的用SQL 语句不能写这样长吗 ?
      

  6.   

    呵呵,你有笔误:Select PoductName,
           Product_Num+(Case when (Select sum(Ptoduct_Num)  From StorIn Where  ProductName=StoreAll.ProdutName) is null then 0 else 
                                  (select sum(Product_Num) from StorIn where 
    ProductName=StoreAll.Name) end ) Product_Num //end )你漏了括号
    from StoreAll
    Union
     Selec ProductName, sum(Product_Num) Product_Num
    from StorIn
    where ProductName not in (select distinct Product_Name from StoreAll)
    Group by ProductName
      

  7.   

    我的老天爷啊, 你好多单词错误啊。我给你修正了一下表: StoreAll , StoreIn字段: ProductName, Product_NumSelect ProductName,
           Product_Num+(Case when (Select sum(Product_Num)  From StoreIn Where  ProductName=StoreAll.ProductName) is null then 0 else 
                                  (select sum(Product_Num) from StoreIn where 
    ProductName=StoreAll.ProductName) end ) Product_Num 
    from StoreAll
    Union
     Select ProductName, sum(Product_Num) Product_Num
    from StoreIn
    where ProductName not in (select distinct ProductName from StoreAll)
    Group by ProductName
      

  8.   

    是,我忘了个括号。。,太粗心了 但是加上之后 还是提示那样的: 未指定错误,我太郁闷了 
    这是不是Delphi 编译器的问题啊怎还出现这样的错误。。还得麻烦您,,谢谢
      

  9.   

    建议以后再把SQL语句写到应用程序里面去(虽然这种方法很不好)之前, 先在查询分析器里面运行一下。
      

  10.   

    修正后的:表: StoreAll , StoreIn字段: ProductName, Product_NumSelect ProductName,
           Product_Num+(Case when (Select sum(Product_Num)  From StoreIn Where  ProductName=StoreAll.ProductName) is null then 0 else 
                                  (select sum(Product_Num) from StoreIn where 
    ProductName=StoreAll.ProductName) end ) Product_Num 
    from StoreAll
    Union
     Select ProductName, sum(Product_Num) Product_Num
    from StoreIn
    where ProductName not in (select distinct ProductName from StoreAll)
    Group by ProductName用ADODataset测试运行通过。
      

  11.   

    To:老冯 十分感谢 感谢 您一句一句的纠正语句单词的错误
    终于发现原来语句中的很多错的单词 
    现将修改好的语句如下, 
    select ProductName,
             
          Product_Num+(Case when (Select sum(Product_Num)  From StorIn Where      ProductName=StoreAll.ProductName) is null then 0 else  
                                  (select sum(Product_Num)  from StorIn where 
         ProductName=StoreAll.ProductName) end) Product_Num
     from StoreAllUnionSelect ProductName,sum(Product_Num) Product_Numfrom StorInwhere ProductName not in (select distinct ProductName from StoreAll)Group by ProductName还得 您看看,我发现是没有 错误了  
    执行之后 还是提示: 未指定错误 啊  
      

  12.   

    对了 那个表的名字是 StorIn 当时建表的时候r 后的e 没有 写
      

  13.   

    哈哈哈哈, ACCESS不支持嵌套查询。
      

  14.   

    如果用ACCESS和是不是没有其他的办法了看来我必须用SQLServer了啊  再在SQLSERVer中建库 刚才我用delphi试着连接了一下SQLSERver数据库 
    用的是TADOConnection ADOQuery TDBGrid 来连接 当在“连接”的选项卡上设置
    “3.在服务器上选择数据库”这项时 出现了“无效的授权说明” 不知是怎么回事
      

  15.   

    MicroSoft OLE DB Provider For SQL Server --->>Next ---> ...
      

  16.   

    SQLSERVER企业管理器上的——控制台根目录
                               -  Microsoft SQL Servers
                                 -MR服务器组
                                   +(LOCAL )(windowsNT)
                                 -SQl SERVER组
                                      (无项目)(MR服务器组是新建的服务器组)是不是 问题出在了 SQLSERVER组下没有项目 这个问题上阿