SQL 中ANY和ALL的用法

来源:百度知道 编辑:UC知道 时间:2024/07/07 05:34:52
最好有例子说明一下。谢谢!
我有三张表,分别存放运动员信息,场地信息和比赛信息,现在要查找至少参加了运动员张三所参加的所有项目的其他运动员名单。Create table athlete
(Ano char(4) not null,
Aname char(8) not null,
Asex char(4),
Adept char(8) not null,
PRIMARY KEY(Ano)
)
create table item
(Ino char(4) not null,
Iname char(12),
Iplace char(8),
PRIMARY KEY(Ino)
)
create table grade
(Ano char(4) not null,
Ino char(4) not null,
Gintegral smallint CHECK (Gintegral in (null,'6','4','2','0')),
FOREIGN KEY(Ano) references athlete(Ano),
FOREIGN KEY(Ino) references item(Ino),
PRIMARY KEY(Ano,Ino)
)
select DISTINCT Aname
from athlete,grade,item
where athlete.Ano=grade.Ano and item.Ino=grade.Ino and Iname <any(
select DISTINCT Iname
from grade,athlete,item
where athlete.Ano=grade.Ano and ite

any表示任意一个,all表示所有的。
如果有张学生记录表student中有一个属性组为age
现在要查找年龄在某个区间上的学生记录就有如下操作
1、查找年龄比15、16、22、21、17、18、19中任意一个都小的学生记录就有如下代码:
select *
from student
where age<any(15,16,22,21,17,18,19)

2、查找年龄比15、16、22、21、17、18、19中任意一个都大的学生记录就有如下代码:
select *
from student
where age>any(15,16,22,21,17,18,19)
/*这里用any 和all是等效的*/用all是大于所有的意思
用all就改为:
where age>all(15,16,22,21,17,18,19)

这里<any就是iname小于括号内部那个子查询所的结果中的任意一个值,也就是说小于其中最小的一个。