table1
name1 sex1table2
name2 sex2用到子查询
select name from table1 where name in(select name2 from table2)现在想得到table1和table2下的所有字段也就是说如何将table2里面的sex2也写出来就得用子查询
name1 sex1table2
name2 sex2用到子查询
select name from table1 where name in(select name2 from table2)现在想得到table1和table2下的所有字段也就是说如何将table2里面的sex2也写出来就得用子查询
from
table1 a, table2 b
where
a.name1 = b.name2
这样找到的是全部的sex2吧 并不是满足条件的sex2
我修改一下。select t1.name,t2.sex2 from table1 t1,table2 t2 where t2.name2=t1.name,这样就行了。
select t1.name,t2.name2,t2.sex2 from table1 t1,table2 t2 where t2.name2=t1.name,这样应该可以满足你的需求了吧。
select t1.name,t1.sex from table1 t1 where t1.name in(select t2.name2 from table2 t2) and t1.sex in (select t3.sex from table2 t3)
这样不就达到楼主想要的效果了吗?
这样就可以了嘛·
改一下 select table1.name1, * from table1 , table2 where table1.name1=table2.name2
select name, (select distinct b.sex2 from table2 b where b.name2 = a.name) as sex2 from table1 a where name in(select name2 from table2)
left join table2 t2
on t1.name=t2.name