select 中的一个简单的SQL语句,

来源:百度知道 编辑:UC知道 时间:2024/05/22 15:35:23
select top 5 c_name,c_stu from class where c_stu>30,c_type=true and c_name like'二班' order by c_stu asc ,order by c_type desc
能告诉我,这个错在哪了吗,
我怎么也调不来了,
我都试了,
把完整的题目告诉大家吧,
请写出在class表中查找满足如下条件的记录的SQL语句:
1.返回字段 c_name,c_stu
2.返回记录数:前5条
3.查询条件:c_stu值 大于30,并且 c_type值为真,并且c_name字段中有“二班”两个字
4.查询结果按c_stu正排序,按c_type倒排序(注:在正排序时请不要省略排序关键字)
是在Access中

"select top 5 c_name,c_stu from class where c_stu>30,c_type=true and c_name like'二班' order by c_stu asc ,order by c_type desc "
这个SQL有3处错误:
1、c_stu>30,c_type=true:中间的“,”换成and 或or(根据你的需要);
2、c_name like'二班':在大部分数据库中,应该是:c_name like '%二班%'(Access数据库中是用*号);
3、order by c_stu asc ,order by c_type desc:应该是:order by c_stu, c_type desc
在某些数据库环境下,c_type=true 也可能是错误的,但是因为不知道你使用的是什么数据库,这个就先不算了。

因此完整的SQL是:
select top 5 c_name,c_stu
from class
where c_stu>30
and c_type=true
and c_name like "*二班*"
order by c_stu asc,c_type desc

select top 5 c_name,c_stu
from class
where c_stu>30
and c_type=true
and c_name like '%二班%'
order by c_stu asc,c_type desc

使用like要使用通配符,看一下SQL的通配符。除了%(百分号)还有一些别的。

select top 5 c_name,c_stu
from class
where c_stu>30
and c_type=true
and c_name like '二班'
or