mysql -3练习(分组查询后再次筛选,顺逆序排序)

插入如下的表格

/*插入表格*/
CREATE TABLE sanguo(
  empno   INT,
  ename   VARCHAR(20),
  job     VARCHAR(20),
  mgr      INT,
  hiredte   DATE,
  sal       DOUBLE,
  comm       DOUBLE,
  deptno     INT
  );
  SELECT * FROM sanguo;

  ALTER TABLE sanguo CHANGE COLUMN hiredte hiredate DATE;
  /*填入数据*/
  INSERT INTO sanguo VALUES(1001,‘甘宁‘,‘文员‘,1013,‘20001217‘,8000.00,NULL,20);
  INSERT INTO sanguo VALUES(1002,‘戴绮斯‘,‘销售员‘,1006,‘2001-02-20‘,16000.00,3000.00,30);
  INSERT INTO sanguo VALUES(1003,‘殷天正‘,‘销售员‘,1006,‘2001-02-22‘,12500.00,5000.00,30);
  INSERT INTO sanguo VALUES(1004,‘刘备‘,‘经理‘,1009,‘2001-04-02‘,29750.00,NULL,20);
  INSERT INTO sanguo VALUES(1005,‘谢逊‘,‘销售员‘,1006,‘2001-09-28‘,12500.00,14000.00,30);
  INSERT INTO sanguo VALUES(1006,‘关羽‘,‘经理‘,1009,‘2001-05-01‘,28500.00,NULL,30);
  INSERT INTO sanguo VALUES(1007,‘张飞‘,‘经理‘,1009,‘2001-09-01‘,24500.00,NULL,10);
  INSERT INTO sanguo VALUES(1008,‘诸葛亮‘,‘分析师‘,1004,‘2007-04-19‘,30000.00,NULL,20);
  INSERT INTO sanguo VALUES(1009,‘曾阿牛‘,‘董事长‘,NULL,‘2001-11-17‘,50000.00,NULL,10);
  INSERT INTO sanguo VALUES(1010,‘韦一笑‘,‘销售员‘,1006,‘2001-09-08‘,15000.00,0.00,30);
  INSERT INTO sanguo VALUES(1011,‘周泰‘,‘文员‘,1008,‘2007-05-23‘,11000.00,NULL,20);
  INSERT INTO sanguo VALUES(1012,‘程普‘,‘文员‘,1006,‘2001-12-03‘,9500.00,NULL,30);
  INSERT INTO sanguo VALUES(1013,‘庞统‘,‘分析师‘,1004,‘2001-12-03‘,30000.00,NULL,20);
  INSERT INTO sanguo VALUES(1014,‘黄盖‘,‘文员‘,1007,‘2002-01-23‘,13000.00,NULL,10);
  INSERT INTO sanguo VALUES(1015,‘张三‘,‘保洁员‘,1001,‘2013-05-01‘,80000.00,50000.00,50);

  /*查询所有部门编号为30的员工*/
  SELECT * FROM sanguo WHERE deptno = 30;
  /*查询所有职位为销售员的员工的编号,姓名,部门编号*/
  SELECT empno,ename,deptno FROM sanguo WHERE job = ‘销售员‘;
  /*查询所有奖金大于工资的员工*/
  SELECT * FROM sanguo WHERE comm > sal ;
  /*查询所有奖金大于工资60%的员工*/
  SELECT * FROM sanguo WHERE comm > (0.6*sal);
  /*查询所有部门编号为10同时是经理或者部门编号为20同时是销售员的员工*/
  SELECT * FROM sanguo WHERE ( deptno = 10 AND job = ‘经理‘) OR (deptno = 20 AND job = ‘销售员‘ );
  /*查询所有部门编号为10同时是经理或者部门编号为20同时是销售员或奖金大于20000的员工*/
  SELECT * FROM sanguo WHERE ( deptno = 10 AND job = ‘经理‘) OR (deptno = 20 AND job = ‘销售员‘ )OR(comm >= 20000);
   /*查询所有没有奖金或奖金小于1000的员工*/
  SELECT ename FROM sanguo WHERE comm IS NULL OR comm < 1000;
   /*查询所有名字是三个字的员工*/
  SELECT ename FROM sanguo WHERE ename LIKE ‘___‘;
   /*查询所有2000年加入公司的员工*/
  SELECT * FROM sanguo WHERE hiredate LIKE ‘2000%‘;
    /*查询所有员工详细信息,用编号升序排序*/
  SELECT * FROM sanguo ORDER BY empno ASC;
   /*查询所有员工详细信息,用工资降序排序,如果工资相同使用入职日期升序排序*/
  SELECT * FROM sanguo ORDER BY sal DESC,hiredate ASC;
   /*查询姓周的两个名字的员工。*/
  SELECT * FROM sanguo WHERE ename LIKE ‘周_‘;
  /*查询所有姓张的员工。*/
  SELECT * FROM sanguo WHERE ename LIKE ‘张%‘;
  /*查询该部门有多少个岗位,每个岗位有多少人。*/
  SELECT job,COUNT(*) FROM sanguo GROUP BY job;
  /*查询哪个岗位中人数大于3。*/
  SELECT job FROM sanguo GROUP BY job HAVING COUNT(*)>3;

原文地址:https://www.cnblogs.com/zhangrui0328/p/8990894.html

时间: 2024-11-09 03:54:24

mysql -3练习(分组查询后再次筛选,顺逆序排序)的相关文章

mysql group by 分组查询

请看下表: 问题: 把统计时间(statistic)相同的,服务域(service_domain_moid) a中的hd100的数量的和与hd100s的数量的和计算出来. 使用方法: group by 首先,我们把service_domain_moid为a的筛选出来,如下:  select * from statistic where service_domain_moid="a"; 其次: 我们把统计时间(statistic_time)相同的分组,求和 select sum(hd10

mysql 按天分组查询

Mysql按天分组(按日分组) select day (日期字段), SUM( 用于合计的字段)from 表group by day (日期字段) 一.年度查询 查询 本年度的数据 SELECT * FROM blog_article WHERE year( FROM_UNIXTIME( BlogCreateTime ) ) = year( curdate( )) 二.查询季度数据 查询数据附带季度数 SELECT ArticleId, quarter( FROM_UNIXTIME( `Blog

Mysql按时间段分组查询来统计会员的个数

1.使用case when方法(不建议使用)-  代码如下 复制代码 SELECT    COUNT(DISTINCT user_id) user_count,    CASE    WHEN create_time>1395046800 AND create_time<1395050400 THEN '17:00-18:00'    WHEN create_time>1395050400 AND create_time<1395054000 THEN '18:00-19:00' 

oracle Group by 分组查询后,分页

public Map getInWareHouseReport(int i, int j, InWareHouse inWareHouse) {        //查询分组后总条数        String countquery="select WS_SKU_ID,sum(IN_NUMBER),sum(WH_MONEY) from IN_WAREHOUSE p group by WS_SKU_ID";        //计算结束行        int m=i+j;        /

mysql 多次分组查询 数据最大的一行

SELECT B, D, Max(E)FROM `总表`WHERE B = '张士建'GROUP BY B, D 通过查询创建工具 编写查询语句 原文地址:https://www.cnblogs.com/gaidalou/p/10412083.html

【mysql】【分组】后取每组的top2

1 DROP TABLE IF EXISTS `tb1`; 2 CREATE TABLE `tb1` ( 3 `id` int(11) NOT NULL AUTO_INCREMENT, 4 `a` varchar(255) DEFAULT NULL, 5 `b` varchar(255) DEFAULT NULL, 6 `content` varchar(255) DEFAULT NULL, 7 PRIMARY KEY (`id`) 8 ) 9 -- ----------------------

Oracle和MySQL分组查询GROUP BY

Oracle和MySQL分组查询GROUP BY 真题1.Oracle和MySQL中的分组(GROUP BY)有什么区别? 答案:Oracle对于GROUP BY是严格的,所有要SELECT出来的字段必须在GROUP BY后边出现,否则会报错:“ORA-00979: not a GROUP BY expression”.而MySQL则不同,如果SELECT出来的字段在GROUP BY后面没有出现,那么会随机取出一个值,而这样查询出来的数据不准确,语义也不明确.所以,作者建议在写SQL语句的时候,

三、mysql分组查询

功能:使用group by子句将表中的数据分成若干组 语法: SELECT column,group_function(column) FROM table [WHERE  condition] #分组前的筛选 [GROUP BY   group_by_expression] [having condition]  # 分组后的筛选 [ORDER BY   column]; 特点: 1) 分组查询中的筛选条件分为两类 数据源 位置 关键字 分组前筛选 原始表 Group by 子句的前面 whe

[Mysql 查询语句]——分组查询group by

#group by(1) group by的含义:将查询结果按照1个或多个字段进行分组,字段值相同的为一组(2) group by可用于单个字段分组,也可用于多个字段分组 select * from employee; +------+------+--------+------+------+-------------+ | num | d_id | name | age | sex | homeaddr | +------+------+--------+------+------+----