当前位置:编程学习 > JAVA >>

询问一条简单的sql语句。。

现在有两个表班级表,学生表 class表,stu表
class里有 classid , name , dec
stu里有   stuid ,classid,stuanme
其中 注意两者都有classid字段

我想通过两个共有的classid,查询出某个班级的所有学生 也就是stu表的stuanme

不要用join on ,inner join, on join in之类的语句 考虑到效率问题。。该怎么写啊?

--------------------编程问答-------------------- select stuname from stu where classid=(select classid from class where name='你想查询的班级')

两表联查。试试看  --------------------编程问答-------------------- 你想查出有课的所有学生? 即class表里有记录的所有学生的stuaname?
join一定效率不好吗? --------------------编程问答-------------------- select stuname from stu where classid in (select classid from class where name='你想查询的班级') --------------------编程问答-------------------- select s.stuname from class c,stu s where c.classid=s.classid and s.classid=你要查询的班级 --------------------编程问答--------------------
引用 1 楼  的回复:
select stuname from stu where classid=(select classid from class where name='你想查询的班级')

两表联查。试试看

+10086 --------------------编程问答-------------------- 其实in的效率不一定比join来的高。。。 --------------------编程问答-------------------- 其实用那些= in的效率还没联合查询那么高,在使用联合查询的时候可以把那些用不上的忽略查询就是了。 --------------------编程问答-------------------- class里有 classid , name , dec
stu里有 stuid ,classid,stuanme


select * from stu where exists(select null from class where class.classid = stu.classid and class.name = '你要查的班级名')

用 exists 效率 比 in 高 --------------------编程问答-------------------- select * from stu where exists (select 1 from class where class.name='...' and class.classid = stu.classid );
补充:Java ,  Java SE
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,