一直没用到自身连接查询,今天做习题,遇到了,不多说,上代码:

*题目:

查询和李军同性别并同班的同学SNAME

 

这是用到的表

2222

土办法:

Select sname from student where ssex=(select ssex fromstudent where sname=’张飞’)and

Class=(select class from student where snam=”张飞”)    

 

自身连接查询:

select s1.sname,s1.ssex,s1.class fromstudent s1,student s2

where s1.ssex=s2.ssex and s1.class=s2.classand s2.sname='张飞'

 

优缺点一目了然

 

Logo

更多推荐