万能公式:Selectfromwheregorup byhavingorder by into table
Zg:职工表 ck:仓库表 chufang:处方表 yisheng:医生表 yao:药表 gys:供应商
一、SQL简单查询1
1、从职工关系中检索所有的工资。
Select 工资from gz     
2、检索仓库中所有的记录。 
Select * from ck   
3、检索工资多于1230元的所有职工号。 
Select 职工号from zg where  工资>1230
4、检索哪些仓库有工资多余1210元的职工。
Select distinct 仓库号from zg where 工资>1210(去掉多余重复的记录)
二、SQL简单查询2
1. 从医生关系中检索所有医生的姓名和职称信息
Select 姓名,职称 from yisheng
2. 列出开处方的医生的职工号(相同只显示1个)
Select distinct 职工号  from chufang
3. 检索药品库的所有信息 
Select * from yao                 
4. 查询职称为主任医师的所有医生信息 
Select * from yisheng where 职称=主任医师
5. 检索哪些医生在997月为病人开了处方,并显示所开处方医生的职工号,日期和处方号。
Select 职工号,日期,处方号  from chufang where 日期>={^1999/07/01and日期<={^1999/07/31}(或者:where year(日期)=1999 and month(日期)=7
三、SQL联接查询1
常用的sql查询语句有哪些1、检索工资多余1230元的职工工号和他们所在的城市
Select 职工号,城市 from zg ,ck where 工资 >1230 and ck.仓库号=zg.仓库号
2、出工作面积大于400的仓库的职工号与这些职工所在的城市
Select 职工号,城市 from ck,zg where 面积>400 and ck.仓库号=zg.仓库号
四、SQL联接查询2
1. 杨阳所开处方的处方号和日期 
Select 处方号,日期 from chufang , yisheng where 姓名=杨阳 and yisheng.职工号=chufang.职工号
2. 查询在处方当中用了同仁堂药品的药编号、药名、处方号、单价信息
Select yao.药编号,药名,处方号 单价 from chufang,yao where 生产厂=同仁堂  and  yao.药编号=chufang.药编号
3. 出所开处方中单价小于10元的处方号、医生姓名、药名、单价、数量信息
Select 处方号,姓名,药名,单价,数量 from yisheng,chufang,yao where yao.单价<10 and yisheng.职工号=chufang.职工号 and  chufang.药编号=yao.药编号
4. 查询由主任医师开的处方号和药品信息
Select 处方号,药名 from yisheng,chufang,yao where 职称=主任医师 and yisheng.职工号=chufang.职工号 and  chufang.药编号=yao.药编号
五、SQL嵌套查询1
1. 有哪些医生至少在19997月开过一次处方,显示医生的姓名。
Select 姓名from yisheng where 职工号 in(sele 职工号 from chufang where year(日期)=1999 and month (日期)=7)
2. 查询由主任医师所开的处方,并显示处方的所有信息。
Select * from chufang where 职工号 in(select 职工号 from yisheng where 职称=主任医师)
六、SQL特殊运算查询1
1. 检索出工资在12201240元的范围职工信息
Select * from zg where 工资>=1220 and 工资<=1240(或者:工资between 1220 and 1240)
2. 从供应商管理中检索所有的供应商公司的信息
Select * from gys where 供应商号 like %公司%为英文状态下)
(“%”为全部替代,“_”为替代单个,中文的%和英文的%是有区别的)
3. 在职工表中把工资从低到高排列
Select * from zg order by 工资(默认升序)
Select * from zg order by 工资 desc(降序)
4. 检索各仓库工资的情况(先按仓库号升序,当仓库号相同时,按工资降序排序)
Select * from zg order by 仓库号,工资 desc
5、计算职工表中有多少个仓库
Select count(dist 仓库号) from zg
6、统计工资求和
Select sum(工资) as 总共工资 from zg
(其余的平均工资avg、最大值max、最小值min做相应改变即可)
7、求每个仓库的最高工资
Select 仓库号 max(工资) as 最高工资from zg group by 仓库号 having count (仓库号) >=2having只能与 group by配合使用)
七、SQL特殊运算查询2
1. 检索出单价在510元的药品信息
Select * from yao where 单价between 5 and 10(或者:单价>=5 and单价<=10
2. 从药品库中检索出所有制药厂的信息
Select * from yao where 生产厂 like %制药厂
3. 出不是以盒为单位的药品信息
Select * from yao where 单位〈〉
4. 按药品单价降序显示全部药品信息
Select * from yao order by 单价 desc
八、SQL计算查询1
1. 统计在997月份开处方的医生人数
Select count (职工号) from chufang where 日期 between^1999/07/01and^1999/07/31
2. 计算由同仁堂生产药品的平均价格
Select 生产厂(或者:同仁堂药厂的药品),avg(单价) as 平均价格 from yao where 生产厂=同仁堂
3. 在处方库中统计每个医生所开的处方数
Select 职工号 count(*) as 开处方数 from chufang group by 职工号
4. 显示至少开过三个(包括三个)处方的医生职工号,处方数。
Select 职工号 count(*) as 开处方数from chufang group by 职工号 having count (*) >=3
SQL附表