+ -
当前位置:首页 → 问答吧 → 请教一个表关联的问题

请教一个表关联的问题

时间:2011-12-08

来源:互联网

tb1 tb2  
根据id关联
tb2有 id,value,stat
当tb2的value=‘1’或‘2’ stat<>‘x'
则主表不显示这条记录

比如现在id=001
tb2中 
id=001 value=1 stat=’‘
id=001 value=3 stat=’x'
怎么能让 主表不显示 id=001这条数据

作者: tianshibuhuifei   发布时间: 2011-12-08

where 条件

作者: xqx_8888   发布时间: 2011-12-08

SQL code

select *
from tb1
inner join tb2 on tb1.id=tb2.id
where not(tb2.value in (1,2) and tb2.stat<>'x')

作者: ap0405140   发布时间: 2011-12-08

select tb1.*
from tb1 join tb2 on tb1.id=tb2.id
where not (value=‘1’or value‘2’ and stat<>‘x')

作者: HEROWANG   发布时间: 2011-12-08

select *
from tb1
inner join tb2 on tb1.id=tb2.id
where not in(tb2.value in (1,2) and tb2.stat<>'x')

作者: szstephenzhou   发布时间: 2011-12-08

SQL code
select
  *
from 
  tb1 a,tb2 b 
where
  a.id=b.id
and
  b.value not in (1,2) and b.stat<>'x'

作者: fredrickhu   发布时间: 2011-12-08

SQL code
select
  *
from 
  tb1 a,tb2 b 
where
  a.id=b.id
and
  b.value not in (1,2) or b.stat<>'x'

作者: fredrickhu   发布时间: 2011-12-08

引用 6 楼 fredrickhu 的回复:
SQL code

select
*
from
tb1 a,tb2 b
where
a.id=b.id
and
b.value not in (1,2) or b.stat<>'x'

林妹妹前辈,这种写法 当b表中 id有多条数据
有not in (1,2) or b.stat<>'x'
也有in (1,2) or b.stat<>'x'
这样还是能关联到的 主表还存在的啊

作者: tianshibuhuifei   发布时间: 2011-12-08

各位,好像不行啊!

作者: tianshibuhuifei   发布时间: 2011-12-08

請提供測試的表數據和想要的結果?

作者: ap0405140   发布时间: 2011-12-08

CREATE TABLE [dbo].[tb1](
[id] [int] NOT NULL,
[name] [varchar](50) COLLATE Chinese_PRC_CI_AS NULL
) ON [PRIMARY]


CREATE TABLE [dbo].[tb2](
[id] [int] NOT NULL,
[value] [varchar](50) COLLATE Chinese_PRC_CI_AS NULL,
[stat] [varchar](50) COLLATE Chinese_PRC_CI_AS NULL
) ON [PRIMARY]

这是两张表

作者: tianshibuhuifei   发布时间: 2011-12-08

insert into tb1 (id,name) values(1,'sel')

insert into tb2 (id,value,stat) values(1,'1','')
insert into tb2 (id,value,stat) values(1,'2','')
insert into tb2 (id,value,stat) values(1,'3','x')


作者: tianshibuhuifei   发布时间: 2011-12-08

根据id关联

假如tb2中,有一条语句是满足 value='1','2' 且 stat<>'x'的  

主表中就不显示这个条数据

也就是说 假如id=1中 有value='1','2' 且 stat<>'x'的 ,id=1这条数据就不用显示了

现在问题是 tb2中 有value='1','2' 且 stat<>'x'的

也有value<>'1','2'且stat='x'的

这个咋弄

作者: tianshibuhuifei   发布时间: 2011-12-08