SQL语法帮助

假设student表中存储着学生的考试成绩,数据信息如下:

IDNameTypeScorePassDate
1 张三 数学 90 2008-09-01
2 李四 语文 93 2007-12-02
3 王五 化学 70 2008-01-10

其中,各列的数据类型如下:
ID  NUMBER  数字类型
Name  VARCHAR2  字符串类型
Type  VARCHAR2  字符串类型
Score  NUMBER  数字类型
PassDate  DATE  日期类型

-----------------------------------------------------------------------------------------

 

数字类型查询

对于数字类型列,可使用的比较符有:=、!=、>、<、>=、<=、between...and...、in、not in
例, 执行以下SQL语句进行查询:

1. select * from student where score = 93
查询结果:

IDNameTypeScorePassDate
2 李四 语文 93 2007-12-02


2. select * from student where score >= 90
查询结果:
IDNameTypeScorePassDate
1 张三 数学 90 2008-09-01
2 李四 语文 93 2007-12-02


3. select * from student where score between 90 and 100
查询结果:
IDNameTypeScorePassDate
1 张三 数学 90 2008-09-01
2 李四 语文 93 2007-12-02

 

-----------------------------------------------------------------------------------------

 

字符串类型查询

对于字符串类型列,在查询时需要在字符串两边添加单引号('),可使用的比较符有:=、!=、like、in、not like、not in
例, 执行以下SQL语句进行查询:

1. select * from student where name = '李四'
查询结果:

IDNameTypeScorePassDate
2 李四 语文 93 2007-12-02


2. 当不确定字符串内容时,可使用like进行查询,其中(%)意为占位符。
select * from student where type like '%%'
查询结果:
IDNameTypeScorePassDate
1 张三 数学 90 2008-09-01


3. 当需要同时查询多个字符串时,可使用in进行查询,其中每个字符串需用(,)隔开。
select * from student where type in ('语文', '化学')
查询结果:
IDNameTypeScorePassDate
2 李四 语文 93 2007-12-02
3 王五 化学 70 2008-01-10

 

-----------------------------------------------------------------------------------------

 

日期类型查询

对于日期类型列,在查询时也需要在日期两边添加单引号('),并且日期格式写法为:01-AUG-08,即2008-08-01(英文简写不区分大小写)。
可使用的比较符有:=、!=、>、<、>=、<=、between ... and ...、like、in、not like、not in
例, 执行以下SQL语句进行查询:

1. select * from student where passdate <= '1-aug-08'
查询结果:

IDNameTypeScorePassDate
2 李四 语文 93 2007-12-02
3 王五 化学 70 2008-01-10


2. 下例通过notlike组合使用,查询日期为07年的数据。
select * from student where passdate not like '%08%'
查询结果:
IDNameTypeScorePassDate
2 李四 语文 93 2007-12-02


3. 当需要查询某个时间段时,可使用between ... and ...进行查询。
select * from student where passdate between '01-Jan-07' and '01-May-08'
查询结果:
IDNameTypeScorePassDate
2 李四 语文 93 2007-12-02
3 王五 化学 70 2008-01-10

 

-----------------------------------------------------------------------------------------

 

多条件查询

在查询过程中,可能会需要输入多个查询条件,这时可使用and、or进行多条件组合。
例, 执行以下SQL语句进行查询:

1. 下例将查询语文考试成绩在90分以上的同学,需要使用and符。
select * from student where type = '语文' and score >= 90
查询结果:

IDNameTypeScorePassDate
2 李四 语文 93 2007-12-02


2. 下例将查询考试成绩在90分以上或60分以下的同学,需要使用or符。
select * from student where score > 90 or score < 60
查询结果:
IDNameTypeScorePassDate
2 李四 语文 93 2007-12-02

 

-----------------------------------------------------------------------------------------
注意:以上蓝色SQL语法均要使用英文半角

posted @ 2008-10-24 10:41  Gnie  阅读(571)  评论(0编辑  收藏  举报
Copyright © 2010 Gnie