select * from 表1 where 表1_id not in (select 中间表表1_id from 中间表 where 中间表表2_id =(select 表2_id from 表2 where 表2一字段="已知"))
怎么可以优化一下。感觉这样写不太好。
select * from 表1 where 表1_id not in (select 中间表表1_id from 中间表 where 中间表表2_id =(select 表2_id from 表2 where 表2一字段="已知"
select t1.*
from 表2 t2
left join 中间表 t on t.表2_id = t2.id
left join 表1 t1 on t.表1_id = t1.id
where t2.字段 = 已知 and t1.id is not null
首先后面这个
中间表表2_id=(
应该是
中间表表2_id in( 吧
然后中间表是一对一还是一对多
select
a.*
from
table_1 a
left join
table_2 b on a.one_id = b.one_id
left join
table_3 c on b.two_id = c.two_id and c.字段 = 已知
where
c.two_id id null
;
t1t2 是中间表,看看这个对你有帮助麽
select t1.*
from t1
left join t1t2 on t1t2.t1id = t1.id
left join t2 on t2.id = t1t2.t2id
where t2.字段 <> '已知'
select *
from 表1
where not exists (select 1
from 中间表, 表2
where 中间表表2.id = 表2.id
and 表2.字段 = '已知'
and 表1.id = 中间表表1.id)
或者
select *
from 表1
where 表1.id not in (
select 中间表表1.id
from 中间表, 表2
where 中间表表2.id = 表2.id
and 表2.字段 = '已知')
Oracle写法版本,请验证采纳,谢谢
select * from t1
where not exist(
select * from t2,t3
where t2.id =t3.id and t2.id =t1.id and t2.字段="已知值"
)
可以使用视图
一:select t1.* from t1 where t1.id not in ( select zj1_id from zj1 join t2 on t2.id = zj2_id where t2f = "已知" ) ;
select *
from 表1
where 表1.id not in (
select 中间表表1.id
from 中间表, 表2
where 中间表表2.id = 表2.id
and 表2.字段 = '已知')
如有帮助,请点击我评论上方【采纳该答案】按钮支持一下,谢谢!
额,sql,增删减查…
我建议你直接拿实际的例子好一点,用表1,表2,而且有些地方还连着写没有分开,看的很痛苦
fanghruyi........ fffffffff.