这个查询怎么实现,在线等待!!!!!!!!!!!!!!!!!!!!!!

huangjianfen 2009-04-01 09:37:39
大家好,我有两个表:T1,T2,两个表的字段是一样的,有7列,但字段A和字段B的数据可能不一样,其余数据一样。
我想查询出表T1里A,B任何一列在表T2中没有的数据。
我是这样写的:
select * from jam_T1
where not exists (select A.* from T1 , T2
where rtrim(T1.A)=rtrim(T2.A) and rtrim(T1.B)=rtrim(T2.B))

首先T1有1000行,
select A.* from T1 ,T2 where rtrim(T1.A)=rtrim(T2.A) and rtrim(T1.B)=rtrim(T2.B)
查出300行

select * from jam_T1
where not exists (select A.* from T1 , T2
where rtrim(T1.A)=rtrim(T2.A) and rtrim(T1.B)=rtrim(T2.B))
却一行都没有,是怎么回事?
...全文
89 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
等不到来世 2009-04-01
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 huangjianfen 的回复:]
可是有一点不明
这个怎么不行
select * from T1
where not exists (select T1.* from T1 a ,T2 b
where rtrim(a.A)=rtrim(b.A) AND rtrim(a.B)=rtrim(b.B) )
[/Quote]
两个T1没任何关联。
要关联起来,使它们指向同一个表,要使用别名。
huangjianfen 2009-04-01
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 mugua604 的回复:]
SQL code
select * from T1 a
where not exists (select * from T2
where rtrim(A)=rtrim(a.A) OR rtrim(B)=rtrim(a.B) )
[/Quote]
应该为:
select * from T1 a
where not exists (select * from T2
where rtrim(A)=rtrim(a.A) AND rtrim(B)=rtrim(a.B) )

OK了
可是有一点不明
这个怎么不行
select * from T1
where not exists (select T1.* from T1 a ,T2 b
where rtrim(a.A)=rtrim(b.A) AND rtrim(a.B)=rtrim(b.B) )

mugua604 2009-04-01
  • 打赏
  • 举报
回复

select * from T1 a
where not exists (select * from T2
where rtrim(A)=rtrim(a.A) OR rtrim(B)=rtrim(a.B) )


huangjianfen 2009-04-01
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 mugua604 的回复:]
SQL code
select * from T1 a
where not exists (select * from T2
where rtrim(A)=rtrim(a.A))
[/Quote]


有两列呀A,B
mugua604 2009-04-01
  • 打赏
  • 举报
回复

select * from T1 a
where not exists (select * from T2
where rtrim(A)=rtrim(a.A))

huangjianfen 2009-04-01
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 hyrongg 的回复:]
SQL code
select * from jam_T1
where not exists (
select 1 FROM T2
where rtrim(T2.A)=rtrim(T1.A) and rtrim(T2.B)=rtrim(T1.B)
)
[/Quote]

这个也不行
huangjianfen 2009-04-01
  • 打赏
  • 举报
回复
写错了,只有两个表,jam_T1应为T1


等不到来世 2009-04-01
  • 打赏
  • 举报
回复
select * from T1  t
where not exists (select 1 from T2 where rtrim(A)=rtrim(t.A) and rtrim(B)=rtrim(t.B))
huangjianfen 2009-04-01
  • 打赏
  • 举报
回复
[Quote=引用楼主 huangjianfen 的帖子:]

select * from jam_T1
where not exists (select A.* from T1 , T2
where rtrim(T1.A)=rtrim(T2.A) and rtrim(T1.B)=rtrim(T2.B))
[/Quote]
写错了:
select * from T1
where not exists (select A.* from T1 , T2
where rtrim(T1.A)=rtrim(T2.A) and rtrim(T1.B)=rtrim(T2.B))


是T1不是jam_T1
htl258_Tony 2009-04-01
  • 打赏
  • 举报
回复
select * from jam_T1 t 
where not exists (select T1.* from T1 , T2
where rtrim(T1.A)=rtrim(T2.A) and rtrim(T1.B)=rtrim(T2.B) and t.ID=t1.ID)
要类似这样.
mugua604 2009-04-01
  • 打赏
  • 举报
回复
[code=SQL]
select A.* from T1 ,T2 where rtrim(T1.A)=rtrim(T2.A) and rtrim(T1.B)=rtrim(T2.B)
--这一步你已经查出有数据存在了

not exists (select A.* from T1 , T2
where rtrim(T1.A)=rtrim(T2.A) and rtrim(T1.B)=rtrim(T2.B))
--这条语句实际上就等于 假了 也可以好比1=0

select * from jam_T1
where not exists (select A.* from T1 , T2
where rtrim(T1.A)=rtrim(T2.A) and rtrim(T1.B)=rtrim(T2.B))
--所以不关jam_T1 里有多少数据
select * from jam_T1 where 1=0 永远都为空
[code]
htl258_Tony 2009-04-01
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 huangjianfen 的回复:]
select * from jam_T1
where not exists (select A.* from T1 , T2
where rtrim(T1.A)=rtrim(T2.A) and rtrim(T1.B)=rtrim(T2.B))

改为

select * from jam_T1
where not exists (select T1.* from T1 , T2
where rtrim(T1.A)=rtrim(T2.A) and rtrim(T1.B)=rtrim(T2.B))
[/Quote]
子查询要跟 jam_T1 建立关联关系.
mugua604 2009-04-01
  • 打赏
  • 举报
回复


select * from jam_T1
where not exists (select A.* from T1 , T2
where rtrim(T1.A)=rtrim(T2.A) and rtrim(T1.B)=rtrim(T2.B))
以上语句应该等于
select * from jam_T1 where 1<>1

false =not exists (select A.* from T1 , T2
where rtrim(T1.A)=rtrim(T2.A) and rtrim(T1.B)=rtrim(T2.B))


mugua604 2009-04-01
  • 打赏
  • 举报
回复


select * from jam_T1
where not exists (select A.* from T1 , T2
where rtrim(T1.A)=rtrim(T2.A) and rtrim(T1.B)=rtrim(T2.B))
以上语句应该等于
select * from jam_T1 where 1<>1

false =not exists (select A.* from T1 , T2
where rtrim(T1.A)=rtrim(T2.A) and rtrim(T1.B)=rtrim(T2.B))


hyrongg 2009-04-01
  • 打赏
  • 举报
回复

select * from jam_T1
where not exists (
select 1 FROM T2
where rtrim(T2.A)=rtrim(T1.A) and rtrim(T2.B)=rtrim(T1.B)
)
huangjianfen 2009-04-01
  • 打赏
  • 举报
回复
select * from jam_T1
where not exists (select A.* from T1 , T2
where rtrim(T1.A)=rtrim(T2.A) and rtrim(T1.B)=rtrim(T2.B))

改为

select * from jam_T1
where not exists (select T1.* from T1 , T2
where rtrim(T1.A)=rtrim(T2.A) and rtrim(T1.B)=rtrim(T2.B))
版本:presto-server-0.214.tar软件版本 presto-cli-0.214-executableCentOS71、presto的起因 hadoop ---hdfs----MR(java)-----hivehive底层原理用MR,速度比较慢,公司hadoop集群主要集中于晚上到凌晨,平日工作时间负载不是很高。但在工作时间内,公司业务人员有实时查询的需求,现在主要借助于hive提供业务人员的查询。hive是基于MR类的SQL查询工具,他会输入的查询SQL解析为MapReduce,能极大的降低使用大数据门槛,让一般的业务人员可以直接准对大数据进行查询,但是有一个利弊,它的查询基于MR,会让人等待比较着急,等待的时间可能是几个小时或者一天。 spark基于内存提高改良的hive,sql,现在factbook在hive上面开发一套利器,准对hive可以通过sql语句快速查询,presto。2、Facebook为何开发Presto  Facebook的2011的数据仓库存储在少量大型hadoopfs集群,Hive是FaceBook在几年前专门为Hadoop打造的一款数据仓库工具,在以前,facebook的科学家和分析师一直靠hive进行数据分析.但hive使用MR作为底层计算框架,是专为批处理设计的,但是随着数据的不断增多,使用hive进行一个简单的数据查询可能要花费分钟或者几个小时,显然不能满足查询需求,FaceBooke也调研了其他比hive更快的工具,但是他们需要在功能有限的条件下做简单操作,以至于无法操作Facebook庞大的数据要求。2012年开始研究自己的框架--presto,每日可以超过1pb查询,而且速度比较快,faceBook声称Presto的性能比hive要好上10倍或者100倍,presto和hive都是facebook开发的 Presto是一个开源的分布式SQL查询引擎,适用于交互式查询,数据量支持GB到PB字节。Presto的设计和编写完全是为了解决Facebook这样规模的商业数据仓库交互式分析和处理速度的问题Presto可以做什么 Presto支持在线数据查询,包括Hive kafka Cassandra关系数据库以及专门数据存储,一条Presto查询可以将多个数据源进行合并,可以跨越整个组织进行分析。Presto以分析师的需求作为目标,他们期望相应速度小于1秒到几分钟,Presto要么在使用速度的快的昂贵的商业方案,提高内存,要么是消耗大量的硬件进行快速查询。(128G 64G)本套课程教给如何在企业环境中使用Presto技术。

22,181

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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