DBgrid 显示两个表中字段值不同的值

DBgrid 显示两个表中字段值不同的值 Delphi / Windows SDK/API
http://www.delphi2007.net/DelphiDB/html/delphi_20061215211111282.html
大家好,新手学习遇到如下问题向各位高手求助,  
  有两个表  
  表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   连接的是前后的字段类型必须相同  
  也尝试了插入子查询   但就是不能得到预想的结果   。。十分苦闷  
  请各大侠指点   。。谢谢  
   
   
   
   
 

简单.   老冯来霸占20分

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

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

非常感谢阿    
  可是当按照你的语句输入到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   呢?  
   
  。。  
 

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

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   语句不能写这样长吗   ?

呵呵,你有笔误:  
   
  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  
   
 

我的老天爷啊,   你好多单词错误啊。我给你修正了一下  
   
  表:   StoreAll   ,   StoreIn  
   
  字段:   ProductName,   Product_Num  
   
  Select   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

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

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

你把我修正后的这段加到程序里面运行看看。

修正后的:  
   
  表:   StoreAll   ,   StoreIn  
   
  字段:   ProductName,   Product_Num  
   
  Select   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测试运行通过。

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   StoreAll  
   
  Union  
   
  Select   ProductName,sum(Product_Num)   Product_Num  
   
  from   StorIn  
   
  where   ProductName   not   in   (select   distinct   ProductName   from   StoreAll)  
   
  Group   by   ProductName  
   
  还得   您看看,我发现是没有   错误了      
  执行之后   还是提示:   未指定错误   啊      
 

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

你后台是不是SQLSERVER?

后台用的是access2003

是不是有影响啊

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

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

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

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

是的   刚才我就是按照您说的那个顺序做的   当时   在“数据连接属性”对话框下设置   :3.在服务器上选择数据库   时   出现了“无效的授权说明”   就连接不下去了

呵呵,     你   再刷新   或重新注册一个。   事情够多的啦。呵呵呵呵

十分感谢你啊   非常感谢   。。。。  
  现在终于发现了一些问题  
  再有问题向您   请教。。感谢。。

在查询分析器里   连接连接sQLserver 时也找不到local,而是一个没有见过的名字 有没有必要卸掉SQLERver重装啊

posted on 2009-05-12 16:00  delphi2007  阅读(248)  评论(0编辑  收藏  举报