有关数据库的一些基本知识

来源:互联网 发布:坚持做一件事 知乎 编辑:程序博客网 时间:2024/05/16 05:07

新建数据库表

create table employee(    eid int(15) primary key auto_increment,    ename varchar(20) not null,    salary float(9,2) not null,    deptid int(15) not null    );

1、用两种方式根据部门号从高到低,工资从低到高列出每个员工的信息

select * from employee order by deptid desc,salary;

这里写图片描述


2、列出各个部门中工资高于本部门的平均工资的员工数和部门号,并按部门号排序

  • 首先,下面这条语句是查询各个部门的平均工资
select avg(salary) from employee group by deptid;

这里写图片描述


order by 与 group by 的区别:

  • order by 是排序;group by 是分组。 具体区别,再写个例子,一看便知:
select * from employee group by deptid desc,salary;

这里写图片描述

select * from employee group by deptid desc;

这里写图片描述

#

0 0
原创粉丝点击