大家好,新手学习遇到如下问题向各位高手求助,
有两个表
表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 连接的是前后的字段类型必须相同
也尝试了插入子查询 但就是不能得到预想的结果 。。十分苦闷
请各大侠指点 。。谢谢
有两个表
表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 连接的是前后的字段类型必须相同
也尝试了插入子查询 但就是不能得到预想的结果 。。十分苦闷
请各大侠指点 。。谢谢
解决方案 »
- 如何将字符串转成类实例
- 一台机器上运行程序是否有可能模拟多个IP地址去某些网页自动投票?
- 一次打开多个文件,并将他们的属性显示在Listview中?
- 如何将teechart的形成的图形保存为bmp文件
- 此号不要送100分,第一个进入者得100分
- 在windows2000下用delphi编程如何访问linux下的Oracle?
- 怎么样能按一个按钮,使decisiongraph1的图形变化样式,如由柱形变成饼形?
- 各位英雄指点晚辈!在线...
- 一个clientdataset的简单问题?
- 能不能实现COMBO BOX象EDIT控件一件READONLY=FALSE类似的状态呢,因为ENABLE = FALSE时太难看了
- TList中能否添加接口?
- 如何用delphi 向网页传递数据
数量+
(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 名称
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 名称
可是当按照你的语句输入到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 呢?。。
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 语句不能写这样长吗 ?
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
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 编译器的问题啊怎还出现这样的错误。。还得麻烦您,,谢谢
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测试运行通过。
终于发现原来语句中的很多错的单词
现将修改好的语句如下,
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还得 您看看,我发现是没有 错误了
执行之后 还是提示: 未指定错误 啊
用的是TADOConnection ADOQuery TDBGrid 来连接 当在“连接”的选项卡上设置
“3.在服务器上选择数据库”这项时 出现了“无效的授权说明” 不知是怎么回事
- Microsoft SQL Servers
-MR服务器组
+(LOCAL )(windowsNT)
-SQl SERVER组
(无项目)(MR服务器组是新建的服务器组)是不是 问题出在了 SQLSERVER组下没有项目 这个问题上阿