请教一个多表联合查询问

suling2002 2011-08-31 04:36:25
TableA Index (key,int) propA1(string), propA2
TableB Index (key,int) propB1(string), propB2

查询语句 1
SELECT COUNT(DISTINCT a.Index) FROM TableA a key join TableB b WHERE a.Index = b.Index and a.propA1 like 'w%' escape '@'

查询语句 2
SELECT COUNT(DISTINCT a.Index) FROM TableA a key join TableB b WHERE a.Index = b.Index and b.propB1 like 'w%' escape '@'

这个两个语句1执行错误SQLCODE=-78内存溢出,
2执行正确。
区别是1查得TableA,2查得时TableB。

在1的后面加上group by a.Index,也是可以运行,但是它的count,不是总和,不是我所想要的。
请诸位大虾帮我分析下。
...全文
121 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
ACMAIN_CHM 2011-09-01
  • 打赏
  • 举报
回复
SELECT * FROM TableA a key join TableB b WHERE a.Index = b.Index

这个的结果有多少条? 如果按照你的"两个表的数据都只有5到六条。" 则应该不超过30条记录。 先检查一下结果。
wwwwb 2011-09-01
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 suling2002 的回复:]
两个表的数据都只有5到六条。

单独执行1 错误提示为
"The following error occur while fetching results:Dynamic memory exhausted SQLCODE=-78,ODBC 3 State = HY001"

但是将语句1中的 COUNT(DISTINCT a.Index)改成*
SELECT * FROM TableA……
[/Quote]
select COUNT(DISTINCT Index) from (
SELECT COUNT(DISTINCT a.Index) FROM TableA a key join TableB b WHERE a.Index = b.Index and a.propA1 like 'w%' escape '@' ) a
能否成功
suling2002 2011-08-31
  • 打赏
  • 举报
回复
两个表的数据都只有5到六条。

单独执行1 错误提示为
"The following error occur while fetching results:Dynamic memory exhausted SQLCODE=-78,ODBC 3 State = HY001"

但是将语句1中的 COUNT(DISTINCT a.Index)改成*
SELECT * FROM TableA a key join TableB b WHERE a.Index = b.Index and a.propA1 like 'w%' escape '@'
这样是可以查询到正确的结果的。
WWWWA 2011-08-31
  • 打赏
  • 举报
回复
单独执行1,能否成功,
两表数据 量多大,索引情况如何
suling2002 2011-08-31
  • 打赏
  • 举报
回复
不是啊
1 是 a.propA1 like 'w%' escape '@'
2 是 b.propB1 like 'w%' escape '@'
在联合后
1是查询表“A”中一个属性”propA1“值是否是以“w”开头
2是查询表“B”中一个属性“propB1”值是否是以“w”开头
WWWWA 2011-08-31
  • 打赏
  • 举报
回复
SELECT COUNT(DISTINCT a.Index) FROM TableA a key join TableB b WHERE a.Index = b.Index and a.propA1 like 'w%' escape '@'

查询语句 2
SELECT COUNT(DISTINCT a.Index) FROM TableA a key join TableB b WHERE a.Index = b.Index and b.propB1 like 'w%' escape '@'

这两个SQL语句 不一样吗?

2,598

社区成员

发帖
与我相关
我的任务
社区描述
Sybase相关技术讨论区
社区管理员
  • Sybase社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧