求一sql语句

wangwenjun0618 2011-05-27 02:29:30
table1
name1 sex1

table2
name2 sex2

用到子查询
select name from table1 where name in(select name2 from table2)

现在想得到table1和table2下的所有字段

也就是说如何将table2里面的sex2也写出来

就得用子查询
...全文
243 30 打赏 收藏 转发到动态 举报
写回复
用AI写文章
30 条回复
切换为时间正序
请发表友善的回复…
发表回复
zuihouyipianyezhi 2011-05-27
  • 打赏
  • 举报
回复
select * from table1 t1
left join table2 t2
on t1.name=t2.name
shenzongyi123 2011-05-27
  • 打赏
  • 举报
回复
有这么复杂么?

select * from table1 t1, table2 t2 where t1.name = t2.name2
Tal 2011-05-27
  • 打赏
  • 举报
回复

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)
fzy1112 2011-05-27
  • 打赏
  • 举报
回复
[Quote=引用 26 楼 fzy1112 的回复:]
select t1.name1, * from table1 , table2 where table1.name1=table2.name2
这样就可以了嘛·
[/Quote]

改一下 select table1.name1, * from table1 , table2 where table1.name1=table2.name2
fzy1112 2011-05-27
  • 打赏
  • 举报
回复
select t1.name1, * from table1 , table2 where table1.name1=table2.name2
这样就可以了嘛·
exterminator 2011-05-27
  • 打赏
  • 举报
回复
[Quote=引用 20 楼 qq363207624 的回复:]

这么简单的问题、至于嘛

Java code

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)
[/Quote]

有想法...
sou784 2011-05-27
  • 打赏
  • 举报
回复
select t1.name1,t1.sex1,t2.sex2 from table1 t1,table2 t2 where t1.name1=t2.name2
这样不就达到楼主想要的效果了吗?
lixin5678 2011-05-27
  • 打赏
  • 举报
回复
同意楼上的说法,数据库设计不合理。
想查同一个姓名是不是有两个性别吗?呵呵
testmelody 2011-05-27
  • 打赏
  • 举报
回复
你说的题目就是有问题的,把name1,name2,sex1,sex2放在一起有什么意义?而且只能查出满足name2的值在name1中都有,那样显示出来的结果不是一样的吗?

select name1,sex1 from table1 where name in(select name2 from table2)
xinwugang 2011-05-27
  • 打赏
  • 举报
回复
联合查询一次性搞定呀!
qq363207624 2011-05-27
  • 打赏
  • 举报
回复
这么简单的问题、至于嘛


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)
lixin5678 2011-05-27
  • 打赏
  • 举报
回复
[Quote=引用 18 楼 wwwhongxia 的回复:]
引用 16 楼 wangwenjun0618 的回复:

引用 15 楼 wwwhongxia 的回复:
假如table1的名字name是1,2,4,5,table2的名字是3,5,所以你select name from table1 where name in(select name2 from table2)和select name2 from table2 where name2 in……
[/Quote]

赫赫,太没创新了,和我的一样
愚公移码 2011-05-27
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 wangwenjun0618 的回复:]

引用 15 楼 wwwhongxia 的回复:
假如table1的名字name是1,2,4,5,table2的名字是3,5,所以你select name from table1 where name in(select name2 from table2)和select name2 from table2 where name2 in(select name from table)查询的结果是……
[/Quote]

select t1.name,t2.name2,t2.sex2 from table1 t1,table2 t2 where t2.name2=t1.name,这样应该可以满足你的需求了吧。
愚公移码 2011-05-27
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 wwwhongxia 的回复:]

假如table1的名字name是1,2,4,5,table2的名字是3,5,所以你select name from table1 where name in(select name2 from table2)和select name2 from table2 where name2 in(select name from table)查询的结果是一样。我给你的查询是select sex2 fro……
[/Quote]

我修改一下。select t1.name,t2.sex2 from table1 t1,table2 t2 where t2.name2=t1.name,这样就行了。
wangwenjun0618 2011-05-27
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 wwwhongxia 的回复:]
假如table1的名字name是1,2,4,5,table2的名字是3,5,所以你select name from table1 where name in(select name2 from table2)和select name2 from table2 where name2 in(select name from table)查询的结果是一样。我给你的查询是select sex2 from……
[/Quote]


嘿嘿 我要现实table1和table2里面的name1和name2
愚公移码 2011-05-27
  • 打赏
  • 举报
回复
假如table1的名字name是1,2,4,5,table2的名字是3,5,所以你select name from table1 where name in(select name2 from table2)和select name2 from table2 where name2 in(select name from table)查询的结果是一样。我给你的查询是select sex2 from table2 where name2 in(select name from table)

wangwenjun0618 2011-05-27
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 shadow4222 的回复:]
select t1.name, t2.sex from table1 t1,table2 t2 where name in(select name2 from table2)
[/Quote]

这样找到的是全部的sex2吧 并不是满足条件的sex2
shadow4222 2011-05-27
  • 打赏
  • 举报
回复
select t1.name, t2.sex from table1 t1,table2 t2 where name in(select name2 from table2)
lixin5678 2011-05-27
  • 打赏
  • 举报
回复
select a.name1,a.sex1,b.name2,a.sex2
from
table1 a, table2 b
where
a.name1 = b.name2
wangwenjun0618 2011-05-27
  • 打赏
  • 举报
回复
列前缀 't2' 与查询中所用的表名或别名不匹配。
加载更多回复(10)

67,512

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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