求一条高效查询语句写法

yzy8788 2011-11-13 08:35:07
有两张表,表结构一模一样
adshowlog
adid,ip,numiid,adtime,uid

adshowlog2
adid,ip,numiid,adtime,uid

想查询adshowlog2中的uid,查询条件是:adshowlog2中的ip、numiid不出现在adshowlog中

拜谢大虾
...全文
165 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
liujuaner 2011-11-14
  • 打赏
  • 举报
回复
select a2.*
from adshowlog2 a2
left join adshowlog a1 on a1.ip = a2.ip and a1.numiid = a2.numiid
where a.adid is null
勿勿 2011-11-14
  • 打赏
  • 举报
回复
select * from adshowlog2 as a where not exists(select 1 from dshowlog where ip=a.ip and numiid=a.numiid)
shuibei_1 2011-11-14
  • 打赏
  • 举报
回复
"IN适合于外表大而内表小的情况;EXISTS适合于外表小而内表大的情况。"
超凡 2011-11-14
  • 打赏
  • 举报
回复
天天回一贴! .....
苦苦的潜行者 2011-11-14
  • 打赏
  • 举报
回复
select * from adshowlog2 as a where not exists (select 1 from adshowlog where ip=a.ip or numiid=a.numiid)
sadikaqy 2011-11-14
  • 打赏
  • 举报
回复

select uid from adshowlog2 a
left join adshowlog b on a.adid=b.adid
where a.ip<>b.ip and a.numiid<>b.numiid
pengxuan 2011-11-13
  • 打赏
  • 举报
回复
使用相关子查询和not exists
大力水手 2011-11-13
  • 打赏
  • 举报
回复

select * from adshowlog2 as a where not exists(select 1 from dshowlog where ip=a.ip and numiid=a.numiid)


大阪正解
yzy8788 2011-11-13
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 geniuswjt 的回复:]

用左连接,去掉null引用 10 楼 yzy8788 的回复:

楼上说的这点我也看到了,就是数据多的时候还是比较慢。
我那种写法就怕逻辑不对,速度还是不慢的。我用的是mysql
join on 应该可以带and条件的吧?
[/Quote]

我最开始像的是用左连接,但是写出来感觉好像不对,我那种写法我感觉也不爽,拐了好几个弯感觉,运行速度10秒左右,逻辑不知道对不对。
koumingjie 2011-11-13
  • 打赏
  • 举报
回复
要查询不存在,一想就是not exists
大版的正解
中国风 2011-11-13
  • 打赏
  • 举报
回复
这样就行了
select * from adshowlog2 as a where not exists(select 1 from dshowlog where  ip=a.ip and numiid=a.numiid)
yzy8788 2011-11-13
  • 打赏
  • 举报
回复
看了楼上的回复,可能我有部分没表达清楚。
这两张表不管uid存不存在,其实只要查询出adshowlog2的记录,判断依据是adshowlog2中的ip、numiid不出现在adshowlog中就行
ningweidong 2011-11-13
  • 打赏
  • 举报
回复

select a2.uip
from adshowlog a, adshowlog2 a2
where a.ip <> a2.ip
and a.numidd <> a2.numidd

你是不是这个意思
中国风 2011-11-13
  • 打赏
  • 举报
回复

--uid和IP和numiid有一项不相同的

select * from adshowlog2 as a where not exists(select 1 from dshowlog where uid=a.uid and ip=a.ip and numiid=a.numiid)
--uid存在,IP和numiid有一项不相同
select * from adshowlog2 as a where not exists(select 1 from dshowlog where uid=a.uid and ip=a.ip and numiid=a.numiid)
and exists(select 1 from dshowlog where uid=a.uid)
a_sking 2011-11-13
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 yzy8788 的回复:]
查询不存在,用not exists效率到底算不算高?
adshowlog数据有40万条左右
adshowlog2数据只有几千
用not exists运行要160秒左右。
而我用下面这样的语句不知道对不对,反正执行效率蛮高的,10秒左右搞定,ip和numiid都有索引的
ps:我用的是.net连接的mysql数据库,发到mssql版来了。我的语句如下,不知道逻辑对不对

SQL cod……
[/Quote] 这个逻辑
geniuswjt 2011-11-13
  • 打赏
  • 举报
回复
用左连接,去掉null[Quote=引用 10 楼 yzy8788 的回复:]

楼上说的这点我也看到了,就是数据多的时候还是比较慢。
我那种写法就怕逻辑不对,速度还是不慢的。我用的是mysql
join on 应该可以带and条件的吧?
[/Quote]
yzy8788 2011-11-13
  • 打赏
  • 举报
回复
楼上说的这点我也看到了,就是数据多的时候还是比较慢。
我那种写法就怕逻辑不对,速度还是不慢的。我用的是mysql
join on 应该可以带and条件的吧?
  • 打赏
  • 举报
回复
exists的查询过程 ,只要有一条查询结果,则终止该次查询,执行下一次查询
yzy8788 2011-11-13
  • 打赏
  • 举报
回复
查询不存在,用not exists效率到底算不算高?
adshowlog数据有40万条左右
adshowlog2数据只有几千
用not exists运行要160秒左右。
而我用下面这样的语句不知道对不对,反正执行效率蛮高的,10秒左右搞定,ip和numiid都有索引的
ps:我用的是.net连接的mysql数据库,发到mssql版来了。我的语句如下,不知道逻辑对不对
select distinct userid from adshowlog2 where userid not in
(
select distinct a.* from (select l.userid from adshowlog2 as l inner join adshowlog r on l.ip=r.ip and l.cnumiid=r.cnumiid) as a
)

上面我虽然用到了in但是adshowlog2 表数据是蛮少的

34,589

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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