与测试共存,与朋友共勉!

SQL 语句实例4

上一篇 / 下一篇  2010-05-15 15:28:20 / 个人分类:转载

SQL语句实例

(2)确定范围

select snamesdeptsage

from student

where sage not between 20 and 23

(3)确定集合

select snamessex

from student

where sdept not in ( 'is''ma''cs' );

(4)字符串匹配

4.1=运算符取代like谓词;!=< >取代not like谓词

% (百分号)代表任意长度(长度可以为0)的字符串

_ (下横线)代表任意单个字符

select snamesnossex

from student

where sname not like '%'

4.2当用户要查询的字符串本身就含有%_时,要使用escape '<换码字符>'短语对通符进行转义。

select *

from course

where cname like 'db\_%i_ _' escape ' \ '

(5)涉及空值的查询

使用谓词is nullis not nullis null”不能用= null”代替

select snocno

from sc

where grade is not null

(6)多重条件查询

and的优先级高于or,可以用括号改变优先级

select sname

from student

where sdept= 'cs' and sage<20

三、对查询结果排序

升序:asc;降序:desc;缺省值为升序

asc:排序列为空值的元组最后显示;desc:排序列为空值的元组最先显示

select *

from student

order by sdeptsage desc

四、使用集函数

计数count[distinct|all] *

count[distinct|all]<列名>

计算总和sum[distinct|all] <列名>

计算平均值avg[distinct|all] <列名>

求最大值max[distinct|all] <列名>

求最小值min[distinct|all] <列名>

distinct短语:在计算时要取消指定列中的重复值

1.select count(distinct sno)

from sc

2.select avg(grade)

from sc

where cno= ' 1 '

五、对查询结果分组

5.1使用group by子句分组

select cnocount(sno)

from sc

group by cno

5.2使用having短语筛选最终输出结果

select sno, count(*)

from sc

where grade>=90

group by sno

having count(*)>=3;

六.连接查询

6.1广义笛卡尔积

select student.* , sc.*

from student, sc

6.2等值连接(含自然连接)

a.等值连接select student.*sc.*

from studentsc

where student.sno = sc.sno

b.自然连接

select student.snosnamessexsagesdeptcnograde

from studentsc

where student.sno = sc.sno

6.3非等值连接查询比较运算符:><>=<=!=

6.4自身连接查询

select s1.snos1.snames1.sdept

from student s1student s2

where s1.sdept = s2.sdept and s2.sname = '刘晨'

6.5外连接查询

select student.sno<SPAN style="COLOR: #333333


TAG:

 

评分:0

我来说两句

我的栏目

日历

« 2024-04-25  
 123456
78910111213
14151617181920
21222324252627
282930    

数据统计

  • 访问量: 14472
  • 日志数: 26
  • 图片数: 1
  • 建立时间: 2007-06-10
  • 更新时间: 2010-05-17

RSS订阅

Open Toolbar