简单查询
SELECT…… FROM…… WHERE……
查询表中指定的字段
select 工资 from 职工
去掉重复的-----distinct
select distinct工资 from 职工
select 职工号,工资 from 职工
查询表中所有字段
select * from 职工
有条件的查询
select distinct 仓库号 from 职工 where 工资>1210
select 职工号 from 职工 where 工资>1230
select 职工号 from 职工 where 工资<1250 and (仓库号="WH1" OR 仓库号="WH2")
select * from 职工 where 工资 >=1220 AND 工资<=1240
between …… AND
select * from 职工 where 工资 between 1220 AND 1240
like ( %和_ )
select * from 供应商 where 供应商名 like "%公司"
select * from 供应商 where 供应商名 like "%厂"
not
select * from 供应商 where 地址!="北京"
select * from 供应商 where 地址<>"北京"
select * from 供应商 where not (地址="北京")
select * from 职工 where 工资 not between 1220 AND 1240
select * from 供应商 where 供应商名 not like "%厂"
简单连接查询
多个关系中含有相同属性名时,必须用关系前缀指明属性所属的关系。否则可不加前缀。
关系名.属性名 或 关系名->属性名
select 职工号,城市 from 职工,仓库;
where 职工.仓库号=仓库.仓库号
select 职工号,城市 from 职工,仓库;
where 职工.仓库号=仓库.仓库号 and 工资>1230
select 职工号,城市 from 职工,仓库;
where 职工->仓库号=仓库->仓库号 and 工资>1230
select 职工号,城市 from 职工,仓库;
where 职工->仓库号=仓库->仓库号 and 面积>400
别名.属性名 或 别名->属性名
select 职工号,城市 from 职工 a,仓库 b;
where a.仓库号=b.仓库号 and 面积>400
嵌套查询
检索关系X中的元组时,它的条件依赖于相关的Y中元组的属性值。
返回单值
查找和职工E4挣同样工资的所有职工(1250)
select 职工号 from 职工 where 工资=;
(select 工资 from 职工 where 职工号="E4")
返回一组值
SELECT 城市 FROM 仓库 WHERE 仓库号 IN;
(SELECT 仓库号 FROM 职工 WHERE 工资=1250)
SELECT 城市 FROM 仓库 WHERE 仓库号 IN ("WH1","WH2")
SELECT * FROM 仓库 WHERE 仓库号 NOT IN;
(SELECT 仓库号 FROM 职工 WHERE 工资<=1210)
SELECT * FROM 仓库 WHERE 仓库号 IN (SELECT 仓库号 FROM 职工) (WH1,WH2,WH3)
SELECT * FROM 仓库 WHERE 仓库号 IN (SELECT 仓库号 FROM 职工);
AND 仓库号 NOT IN;
(SELECT 仓库号 FROM 职工 WHERE 工资<=1210)
或:
SELECT * FROM 仓库 WHERE 仓库号 NOT IN;
(SELECT 仓库号 FROM 职工 WHERE 工资<=1210);
AND 仓库号 IN (SELECT 仓库号 FROM 职工)
排序ORDER BY (ASC DESC )
select * from 职工 ORDER BY 工资
select * from 职工 ORDER BY 工资 desc
select * from 职工 ORDER BY 仓库号,工资 desc
简单计算
sele count(*) from 供应商
sele count(地址) from 供应商(有重复4)
sele count(dist 地址) from 供应商(3)
sele sum(工资),avg(工资),max(工资),min(工资) from 职工
sele sum(工资)as 工资总和,avg(工资)as 工资平均值,max(工资) as 工资最大值,min(工资)as 工资最小值 from 职工
sele max(工资),min(工资) from 职工 where 仓库号="WH2"
sele sum(工资) from 职工 where 仓库号 in;
(sele 仓库号 from 仓库 where 城市="北京" or 城市="上海")
分组
SELECT 仓库号,AVG(工资) FROM 职工 GROUP BY 仓库号
SELECT 仓库号 ,COUNT(*),AVG(工资) FROM 职工;
GROUP BY 仓库号 HAVING COUNT(*)>=2
空值
SELECT * FROM 订购单 WHERE 供应商号 is null
SELECT * FROM 订购单 WHERE 供应商号 is NOT null
别名与自然连接
select 职工号,城市 from 职工 a,仓库 b;
where a.仓库号=b.仓库号
SELECT 供应商名 FROM 供应商 a,订购单 b,职工 c,仓库 d;
WHERE a.供应商号=b.供应商号 AND b.职工号=c.职工号 AND c.仓库号=d.仓库号
内外层互相关嵌套查询
SELECT out.职工号,out.供应商号,out.订购单号,out.订购日期,out.总金额 FROM 订购单 out ;
WHERE 总金额=(select MAX(总金额) FROM 订购单 INNER1 WHERE out.职工号=inner1.职工号)
如果采用分组,则
SELECT 职工号,MAX(总金额) FROM 订购单 GROUP BY 职工号
不能
SELECT 职工号,供应商号,MAX(总金额) FROM 订购单 GROUP BY 职工号
使用量词和谓词的查询
4.27 SELECT * FROM 仓库 WHERE 仓库号 NOT IN ;
(SELECT 仓库号 FROM 职工)
或
SELECT * FROM 仓库 WHERE NOT exists;
(SELECT * FROM 职工 WHERE 仓库号=仓库.仓库号)
4.28
SELECT * FROM 仓库 WHERE exists;
(SELECT * FROM 职工 WHERE 仓库号=仓库.仓库号)
4.29
SELECT DISTINCT 仓库号 FROM 职工 WHERE 工资>=;
ANY(select 工资 FROM 职工 WHERE 仓库号="WH1" )
SELECT DISTINCT 仓库号 FROM 职工 WHERE 工资>=;
(select MIN(工资) FROM 职工 WHERE 仓库号="WH1" )
4.30
SELECT DISTINCT 仓库号 FROM 职工 WHERE 工资>=;
ALL(select 工资 FROM 职工 WHERE 仓库号="WH1" )
SELECT DISTINCT 仓库号 FROM 职工 WHERE 工资>=;
(select MAX(工资) FROM 职工 WHERE 仓库号="WH1" )
超级连结查询
SELECT 仓库.仓库号,城市,面积,职工号 ,工资;
FROM 仓库 INNER JOIN 职工;
on 仓库.仓库号 =职工.仓库号
SELECT 仓库.仓库号,城市,面积,职工号 ,工资;
FROM 仓库 left JOIN 职工;
on 仓库.仓库号 =职工.仓库号
SELECT 仓库.仓库