注册 登录
编程论坛 SQL Server论坛

[多表查询]--求助

landigital 发布于 2006-09-22 21:43, 635 次点击

请写出在class和stu表中查找满足如下条件的记录的sql语句:
1)表class中的字段c_id与stu表中的s_cid为关联字段
2)返回字段 c_name,s_no,s_name
3)查询条件 c_stu 字段值大于20,并且s_no第二到四位是"006"这三个字符
4)查询结果按s_no倒排序

谢了~~~~~

3 回复
#2
LouisXIV2006-09-22 22:19
--test it by yourself please

select c.c_name,s.s_no,s.s_name
from class c inner join stu s
on c.c_id=s.s_cid
where c_stu>20 and substring(rtrim(s_no),2,4)='006'
order by s_no desc
#3
landigital2006-09-22 22:57
以下是引用LouisXIV在2006-9-22 22:19:38的发言:
--test it by yourself please

select c.c_name,s.s_no,s.s_name
from class c inner join stu s
on c.c_id=s.s_cid
where c_stu>20 and substring(rtrim(s_no),2,4)='006'
order by s_no desc

where c_stu>20 and s_no like '_006%'
order by s_no desc
使用模糊查询的关键字like~~~

#4
fengmumei2006-09-27 11:14
select class.c_name,stu.s_no,stu.s_name from class,stu
where class.c_id=stu.s_cid and class.c_stu>20 and stu.s_no like '_006%'
order by stu.s_no desc

首先,不知道你的那两个表里的字段是否相同,所以我把表名都加上了,若是字段不相同,你可把表名去掉
select c_name,s_no,s_name from class,stu
where c_id=s_cid and c_stu>20 and s_no like '_006%'
order by s_no desc
再者,不知道你的字段s_no是几位,所以要用like '_006%'

1