zoukankan      html  css  js  c++  java
  • 高级查询练习题

    /*
    Navicat MySQL Data Transfer

    Source Server : localhost
    Source Server Version : 50617
    Source Host : localhost:3306
    Source Database : ceshi

    Target Server Type : MYSQL
    Target Server Version : 50617
    File Encoding : 65001

    Date: 2018-06-07 09:04:53
    */

    SET FOREIGN_KEY_CHECKS=0;
    -- ----------------------------
    -- Table structure for `orders`
    -- ----------------------------
    DROP TABLE IF EXISTS `orders`;
    CREATE TABLE `orders` (
    `Work_id` varchar(20) NOT NULL,
    `supp_id` varchar(20) NOT NULL,
    `Order_id` varchar(20) DEFAULT NULL,
    `money` varchar(20) NOT NULL,
    PRIMARY KEY (`Work_id`,`supp_id`),
    KEY `supp_id` (`supp_id`),
    CONSTRAINT `orders_ibfk_1` FOREIGN KEY (`Work_id`) REFERENCES `worker` (`Work_id`),
    CONSTRAINT `orders_ibfk_2` FOREIGN KEY (`supp_id`) REFERENCES `supply` (`supp_id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

    -- ----------------------------
    -- Records of orders
    -- ----------------------------
    INSERT INTO `orders` VALUES ('e1', 's4', 'or73', '1000000.00');
    INSERT INTO `orders` VALUES ('e3', 's7', 'or67', '5000000.00');
    INSERT INTO `orders` VALUES ('e6', '', 'or77', '6000000.00');
    INSERT INTO `orders` VALUES ('e7', 's4', 'or76', '8000000.00');

    -- ----------------------------
    -- Table structure for `supply`
    -- ----------------------------
    DROP TABLE IF EXISTS `supply`;
    CREATE TABLE `supply` (
    `supp_id` varchar(20) NOT NULL,
    `supp_name` varchar(20) NOT NULL,
    `supp_area` varchar(20) NOT NULL,
    PRIMARY KEY (`supp_id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

    -- ----------------------------
    -- Records of supply
    -- ----------------------------
    INSERT INTO `supply` VALUES ('', '', '');
    INSERT INTO `supply` VALUES ('s3', '振华电子厂', '西安');
    INSERT INTO `supply` VALUES ('s4', '华通电子公司', '北京');
    INSERT INTO `supply` VALUES ('s6', '607厂', '郑州');
    INSERT INTO `supply` VALUES ('s7', '爱华电子厂', '北京');

    -- ----------------------------
    -- Table structure for `warehouse`
    -- ----------------------------
    DROP TABLE IF EXISTS `warehouse`;
    CREATE TABLE `warehouse` (
    `Ware_id` varchar(20) NOT NULL,
    `city` varchar(20) NOT NULL,
    `area` varchar(20) NOT NULL,
    PRIMARY KEY (`Ware_id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

    -- ----------------------------
    -- Records of warehouse
    -- ----------------------------
    INSERT INTO `warehouse` VALUES ('wh1', '北京', '370');
    INSERT INTO `warehouse` VALUES ('wh2', '上海', '500');
    INSERT INTO `warehouse` VALUES ('wh3', '广州', '200');
    INSERT INTO `warehouse` VALUES ('wh4', '武汉', '400');

    -- ----------------------------
    -- Table structure for `worker`
    -- ----------------------------
    DROP TABLE IF EXISTS `worker`;
    CREATE TABLE `worker` (
    `Work_id` varchar(20) NOT NULL,
    `Ware_id` varchar(20) NOT NULL,
    `wages` varchar(20) NOT NULL,
    PRIMARY KEY (`Work_id`),
    KEY `Ware_id` (`Ware_id`),
    CONSTRAINT `worker_ibfk_1` FOREIGN KEY (`Ware_id`) REFERENCES `warehouse` (`Ware_id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

    -- ----------------------------
    -- Records of worker
    -- ----------------------------
    INSERT INTO `worker` VALUES ('e1', 'wh2', '1220');
    INSERT INTO `worker` VALUES ('e3', 'wh1', '1210');
    INSERT INTO `worker` VALUES ('e4', 'wh2', '1250');
    INSERT INTO `worker` VALUES ('e6', 'wh3', '1230');
    INSERT INTO `worker` VALUES ('e7', 'wh1', '1250');

    --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 订购单表 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'

    ================自己练习=============

    --1.从职工关系中检索所有工资值。
    Select 工资 from 工资表;
    --2.检索仓库关系中的所有记录。
    Select * from warehouse
    --3.检索工资多于1230元的职工号。
    Select 职工号 from worker where 工资 > 1230;
    --4.检索哪些仓库有工资多于1210元的职工。
    Select 仓库号 from worker where 工资 > 1210;
    --5.给出在仓库“wh1”或“wh2”工作,并且工资少于1250元的职工号。
    Select 职工号 from worker where (仓库号=‘wh1’ or 仓库号=‘wh2’)and 工资 < 1250; 
    --6.找出工资多于1230元的职工号和他们所在的城市。
    select 城市,职工号 from 仓库表 join 职工表 on 仓库表.仓库号=职工表.仓库号 where 工资>'1230'
    --7.找出工作在面积大于400的仓库的职工号以及这些职工工作所在的城市。
    select 城市,职工号 from 仓库表 join 职工表 on 仓库表.仓库号=职工表.仓库号 where 面积>'400'; 
    --★8.哪些城市至少有一个仓库的职工工资为1250元。 
    Select 城市 from warehouse where 仓库号 in (select 仓库号 from worker where 工资 = 1250 );
    --9.查询所有职工的工资都多于1210元的仓库的信息。
     select * from 仓库表 where 仓库号 in( select 仓库号 from 职工表 where 1210< all(select 工资 from 职工表 where 仓库表.仓库号=职工表.仓库号))
    --10.找出和职工e4挣同样工资的所有职工。
    Select 职工号 from worker where 工资 = (selet 工资 from worker where 职工号 = ‘e4’);
    --11.检索出工资在1220元到1240元范围内的职工信息1。
    Select * from worker where 工资 between 1220 and 1240;
    --★12.从供应商关系中检索出全部公司的信息,不要工厂或其他供应商的信息。 
    Select * from supply where 供应商 = ‘%公司’; 
    --13.找出不在北京的全部供应商信息。
    Select * from supply where 地址 != 北京;
    --14.按职工的工资值升序检索出全部职工信息。
    Select * from worker order by 工资 asc; 
    --15.先按仓库号排序,再按工资排序并输出全部职工信息。
     select *from 职工表 order by 仓库号 asc , 工资 asc
    --16.找出供应商所在地的数目
    Select count(地址) from supply
    -17.求支付的工资总数。
    Select sum(工资)from worker;
    --18.求北京和上海的仓库职工的工资总和。
    Select sun(工资) from worker where 仓库号 = (select 仓库号 from warehouse where 城市=‘上海’ and 城市 = ‘北京’);
    --19.求所有职工的工资都多于1210元的仓库的平均面积。
    select AVG(面积) from 仓库表 where 仓库号 in (select 仓库号 from 职工表 where 工资>'1210')
    --20.求在wh2仓库工作的职工的最高工资值。
    Select max(工资) from worker where 仓库号 =’wh2’ ;
    --21.求每个仓库的职工的平均工资。
    Select avg(工资) from worker group by 仓库号;
    --22.求至少有两个职工的每个仓库的平均工资。
    Select avg(工资) from worker group by 仓库号 having count(*)>= 2;
    --23.找出尚未确定供应商的订购单。
    Secect * from order where 供货商号 = null;
    --24.列出已经确定了供应商的订购单信息。
    select * from 订购单表 where 供应商号 is not null
    -25.查询供应商名。
    Select * from supply;
    --★26.在订购单表中加入一个新字段总金额,说明完成该订购单所应付出的总金额数。
    Alrter table 订购单表 add 订购金额 varchar(max)
    -27.列出每个职工经手的具有最高总金额的订购单信息。
    select MAX(订购金额) from 订购单表 group by 职工号
    --28.检索哪些仓库中还没有职工的仓库的信息。
    select *from 仓库表 where 仓库号 not in (select 仓库号 from 职工表)
    --29.检索哪些仓库中至少已经有一个职工的仓库的信息。
    Select * from warehouse where 仓库号 = (select 仓库号 from worker group by 仓库号 having count(仓库号)>= 1);
    --★30.检索有职工的工资大于或等于wh1仓库中任何一名职工工资的仓库号。
    Select 仓库号 from worker where工资 > any (select 工资 from worker where 仓库号 = ‘wh1’);
    --★31.检索有职工的工资大于或等于wh1仓库中所有职工工资的仓库号。
    Select 仓库号 from worker where工资 >= all (select 工资 from worker where 仓库号 = ‘wh1’);
  • 相关阅读:
    jQuery火箭图标返回顶部代码
    jQuery火箭图标返回顶部代码
    All strings must be XML compatible: Unicode or ASCII, no NULL bytes or control characters
    记一次odoo创建新的模块时,但是在odoo web界面找不到应用的案例
    python实现格式化输出9*9乘法表
    format和urlencode的使用对比
    python字典小知识
    01
    深浅拷贝再回顾
    DRF的路由生成类的使用
  • 原文地址:https://www.cnblogs.com/zhengleilei/p/9149895.html
Copyright © 2011-2022 走看看