zoukankan      html  css  js  c++  java
  • 数据库 查询例题-----仓库题

    create table 仓库表
    (
     仓库号 varchar(50) primary key not null,
     城市  varchar(50) not null,
     面积  int not null,
    )
    insert into 仓库表 values ('wh1','北京',370)
    insert into 仓库表 values ('wh2','上海',500)
    insert into 仓库表 values ('wh3','广州',200)
    insert into 仓库表 values ('wh4','武汉',400)
    delete from 仓库表
    
    
    
    create table 职工表
    (
     仓库号 varchar(50) not null,
     职工号 varchar(50) primary key not null,
     工资 int  not null
     foreign key(仓库号)
     references 仓库表(仓库号),
    )
    insert into 职工表 values ('wh2','e1',1220)
    insert into 职工表 values ('wh1','e3',1210)
    insert into 职工表 values ('wh2','e4',1250)
    insert into 职工表 values ('wh3','e6',1230)
    insert into 职工表 values ('wh1','e7',1250)
    delete from 职工表
    
    
    
    create table 订购单表
    (
      职工号  varchar(50) not null,
      供应商号   varchar(50) ,
      订购单号 varchar(50) not null,
      订购日期 date 
      foreign key(职工号)
      references 职工表(职工号),
      foreign key(供应商号)
      references 供应商表(供应商号),
    )
    truncate table 订购单表 
    insert into 订购单表 values ('e3','s7','or67','2001-6-23')
    insert into 订购单表 values ('e1','s4','or73','2001-7-28')
    insert into 订购单表 values ('e7','s4','or76','2001-5-25')
    insert into 订购单表 values ('e6',null,'or77',null)
    insert into 订购单表 values ('e3','s4','or79','2001-6-13')
    insert into 订购单表 values ('e1',null,'or80',null)
    insert into 订购单表 values ('e3',null,'or90',null)
    insert into 订购单表 values ('e3','s3','or91','2001-7-13')
    delete from 订购单表
    
    
    
    create table 供应商表
    (
     供应商号 varchar(50) primary key not null ,
     供应商名  varchar(50) not null,
     地址 varchar(50) not null,
    )
    insert into 供应商表 values ('s3','振华电子厂','西安')
    insert into 供应商表 values ('s4','华通电子公司','北京')
    insert into 供应商表 values ('s6','607厂','郑州')
    insert into 供应商表 values ('s7','爱华电子厂','北京')
    delete from 供应商表
    
    
    select*from 仓库表
    select*from 职工表
    select*from 订购单表
    select*from 供应商表
    
    
    
    --1.从职工关系中检索所有工资值。
    select 工资 from 职工表;
    
    --2.检索仓库关系中的所有记录。
    select *from 仓库表;
    
    --3.检索工资多于1230元的职工号。
    select 职工号 from 职工表 where 工资>'1230'; 
    
    --4.检索哪些仓库有工资多于1210元的职工。
    select 仓库号, 职工号 from 职工表 where 工资>'1210' 
    
    --5.给出在仓库“wh1”或“wh2”工作,并且工资少于1250元的职工号。
    select 职工号 from 职工表 where 仓库号 not like 'wh3' and 工资<'1250'
    
    --6.找出工资多于1230元的职工号和他们所在的城市。
    select 城市,职工号 from 仓库表 join 职工表 on 仓库表.仓库号=职工表.仓库号 where 工资>'1230'
    
    --7.找出工作在面积大于400的仓库的职工号以及这些职工工作所在的城市。
    select 城市,职工号 from 仓库表 join 职工表 on 仓库表.仓库号=职工表.仓库号 where 面积>'400'
    
    --★8.哪些城市至少有一个仓库的职工工资为1250元。
    select 城市 from 仓库表 where 仓库号 in (select 仓库号 from 职工表 where 工资='1250')     
    
    
    --9.查询所有职工的工资都多于1210元的仓库的信息。
    select *from 仓库表 where 仓库号 in( select 仓库号 from 职工表 where 1210< all(select 工资 from 职工表 where 仓库表.仓库号=职工表.仓库号))
    
    --10.找出和职工e4挣同样工资的所有职工。
    select 职工号 from 职工表 where 工资 = (select 工资 from 职工表 where 职工号='e4') and 职工号 !='e4'
    
    --11.检索出工资在1220元到1240元范围内的职工信息。
    select*from 仓库表 join 职工表 on 职工表.仓库号=仓库表.仓库号 where 工资 between 1220 and 1240
    
    --★12.从供应商关系中检索出全部公司的信息,不要工厂或其他供应商的信息。
    select*from 订购单表
    select *from 订购单表 a join 职工表 b on a.职工号=b.职工号 and 供应商号=any(select 供应商号 from 供应商表)
    
    --13.找出不在北京的全部供应商信息。
    select * from 供应商表 where 地址 !='北京'
    
    --14.按职工的工资值升序检索出全部职工信息。
    SELECT *from 职工表 order by 工资 asc
    
    --15.先按仓库号排序,再按工资排序并输出全部职工信息。
    select *from 职工表 order by 仓库号 asc , 工资 asc
    
    --16.找出供应商所在地的数目。
    select 地址,COUNT(*) from 供应商表 group by 地址
    select COUNT(地址) from 供应商表 
    
    --17.求支付的工资总数。
    select SUM(工资) from 职工表
    
    --18.求北京和上海的仓库职工的工资总和。
    select SUM(工资) from 职工表 where 仓库号 in (select 仓库号 from 仓库表 where 城市 in('北京','上海')) 
    
    --19.求所有职工的工资都多于1210元的仓库的平均面积。
    select AVG(面积) from 仓库表 where 仓库号 in (select 仓库号 from 职工表 where 工资>'1210')
    
    --20.求在wh2仓库工作的职工的最高工资值。
    select max(工资) from 职工表 where 仓库号 like 'wh2'
    
    --21.求每个仓库的职工的平均工资。
    select AVG(工资) from 职工表 group by 仓库号  
    
    --22.求至少有两个职工的每个仓库的平均工资。
    select AVG(工资) from 职工表  group by 仓库号 having COUNT(仓库号)>=2
    
    --23.找出尚未确定供应商的订购单。
    select *from 订购单表 where 供应商号 is null
    
    --24.列出已经确定了供应商的订购单信息。
    select *from 订购单表 where 供应商号 is not null
    
    --25.查询供应商名。
    select *from 供应商表
    
    --★26.在订购单表中加入一个新字段总金额,说明完成该订购单所应付出的总金额数。
    alter table 订购单表 add 订购金额 varchar(max);
    
    --27.列出每个职工经手的具有最高总金额的订购单信息。
    select MAX(订购金额) from 订购单表 group by 职工号
    
    --28.检索哪些仓库中还没有职工的仓库的信息。
    select *from 仓库表 where 仓库号 not in (select 仓库号 from 职工表) 
    
    --29.检索哪些仓库中至少已经有一个职工的仓库的信息。
    select *from 仓库表 where 仓库号 
    in (select 仓库号 from 职工表 group by 职工号 having COUNT(职工号)>=1)
    
    --★30.检索有职工的工资大于或等于wh1仓库中任何一名职工工资的仓库号。 
    select *from 仓库表 where 仓库号 
    in(select 仓库号 from 职工表 where 职工号 
    in (select 职工号 from 职工表 where 工资
    >(select MIN(工资) from 职工表 where 仓库号 like 'wh1')))
    
    select distinct 仓库号 from 职工表 where 工资>=any(select 工资 from 职工表 where 仓库号='wh1') and 仓库号!='wh1' 
    
    
    --★31.检索有职工的工资大于或等于wh1仓库中所有职工工资的仓库号。
    select *from 仓库表 where 仓库号 
    in(select 仓库号 from 职工表 where 职工号 
    in (select 职工号 from 职工表 where 工资
    >=(select MAX(工资) from 职工表 where 仓库号 like 'wh1')))
    
    select 仓库号 from 职工表 where 工资>= all(select 工资 from 职工表 where 仓库号='wh1') and 仓库号!='wh1' 
    二百个不间断的重复,只是让我看到了人的命运无法改变这一事实而已。
  • 相关阅读:
    python 装饰器
    git
    JS原生方法实现jQuery的ready()
    js获取css属性方法
    列表页调出点击量
    数组操作
    判断IE版本
    判断IE浏览器用IE条件表达式
    [jQuery] Cannot read property ‘msie’ of undefined错误的解决方法
    复选框字段数组拆分后循环选项值,if判断根据选项值,前端输出html
  • 原文地址:https://www.cnblogs.com/dlexia/p/4459426.html
Copyright © 2011-2022 走看看