+ -
当前位置:首页 → 问答吧 → 求一条高效查询语句写法

求一条高效查询语句写法

时间:2011-11-13

来源:互联网

有两张表,表结构一模一样
adshowlog
adid,ip,numiid,adtime,uid

adshowlog2
adid,ip,numiid,adtime,uid

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

拜谢大虾

作者: yzy8788   发布时间: 2011-11-13

SQL code

--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)

作者: roy_88   发布时间: 2011-11-13

SQL code

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


你是不是这个意思

作者: ningweidong   发布时间: 2011-11-13

看了楼上的回复,可能我有部分没表达清楚。
这两张表不管uid存不存在,其实只要查询出adshowlog2的记录,判断依据是adshowlog2中的ip、numiid不出现在adshowlog中就行

作者: yzy8788   发布时间: 2011-11-13

这样就行了
SQL code
select * from adshowlog2 as a where not exists(select 1 from dshowlog where  ip=a.ip and numiid=a.numiid)

作者: roy_88   发布时间: 2011-11-13

要查询不存在,一想就是not exists
大版的正解

作者: koumingjie   发布时间: 2011-11-13

SQL code

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



大阪正解

作者: public0011   发布时间: 2011-11-13

使用相关子查询和not exists

作者: pengxuan   发布时间: 2011-11-13