zoukankan      html  css  js  c++  java
  • MySQL基本操作语句总结

    以下面的题目为例子简单的总结 一下MySQL语句使用:

    The Employee table holds all employees. Every employee has an Id, a salary, and there is also a column for the department Id.

    create table Employee(Id int unsigned not null auto_increment primary key,Name char(8) not null,Salary char(4) not null,DepartmentId int);

    insert into Employee(Id, Name, Salary, DepartmentId) values(’Joe’,’70000’,’1’);

    +----+-------+--------+--------------+
    | Id | Name  | Salary | DepartmentId |
    +----+-------+--------+--------------+
    | 1  | Joe   | 70000  | 1            |
    | 2  | Henry | 80000  | 2            |
    | 3  | Sam   | 60000  | 2            |
    | 4  | Max   | 90000  | 1            |
    +----+-------+--------+--------------+

    The Department table holds all departments of the company.

    +----+----------+
    | Id | Name     |
    +----+----------+
    | 1  | IT       |
    | 2  | Sales    |
    +----+----------+

    Write a SQL query to find employees who have the highest salary in each of the departments. For the above tables, Max has the highest salary in the IT department and Henry has the highest salary in the Sales department.

    +------------+----------+--------+
    | Department | Employee | Salary |
    +------------+----------+--------+
    | IT         | Max      | 90000  |
    | Sales      | Henry    | 80000  |
    +------------+----------+--------+

    在MySQL中构建数据表:

    mysql> usemysql;

    mysql> create table Employee(Id int unsigned not null auto_increment primary key,Name varchar(20) not null,Salary varchar(20) not null,DepartmentId int);

    mysql> insert into Employee values(1,'Joe','70000',1);
    Query OK, 1 row affected (0.04 sec)

    mysql> insert into Employee values(2,'Henry','80000',2),(3,'Sam','60000',2),(4,'Max','90000',1);

    mysql> select * from employee;

    +----+-------+--------+--------------+
    | Id | Name | Salary | DepartmentId |
    +----+-------+--------+--------------+
    | 1 | Joe | 70000 | 1 |
    | 2 | Henry | 80000 | 2 |
    | 3 | Sam | 60000 | 2 |
    | 4 | Max | 90000 | 1 |
    +----+-------+--------+--------------+
    4 rows in set (0.00 sec)

    mysql> create table Department(Id int primary key not Null,Name varchar(20));
    Query OK, 0 rows affected (0.55 sec)

    mysql>
    mysql> insert into Department values(1,'IT'),(2,'Sales');
    Query OK, 2 rows affected (0.03 sec)
    Records: 2 Duplicates: 0 Warnings: 0

    mysql> select * from Department;
    +----+-------+
    | Id | Name |
    +----+-------+
    | 1 | IT |
    | 2 | Sales |
    +----+-------+
    2 rows in set (0.00 sec)

    内连接Employee和Department表:

      mysql> select * from Employee as e inner join Department as d on e.DepartmentId=d.Id;

    +----+-------+--------+--------------+----+-------+
    | Id | Name | Salary | DepartmentId | Id | Name |
    +----+-------+--------+--------------+----+-------+
    | 1 | Joe | 70000 | 1 | 1 | IT |
    | 2 | Henry | 80000 | 2 | 2 | Sales |
    | 3 | Sam | 60000 | 2 | 2 | Sales |
    | 4 | Max | 90000 | 1 | 1 | IT |
    +----+-------+--------+--------------+----+-------+
    4 rows in set (0.00 sec)

    根据部门排序,获取每个部门的最高薪水表:

    mysql> select e.DepartmentId, MAX(e.Salary) as Salary, d.Name as Department from Employee as e inner join Department as d

                on e.DepartmentId = d.Id group by e.DepartmentId;

    +--------------+--------+------------+
    | DepartmentId | Salary | Department |
    +--------------+--------+------------+
    | 1 | 90000 | IT |
    | 2 | 80000 | Sales |
    +--------------+--------+------------+
    2 rows in set (0.00 sec)

    使用上面的部门最高薪资表跟Employee内连接,获取最终的信息:

    mysql> select t.Name as Department,e.Name as Employee, e.Salary as Salary from Employee as e inner join (select e.DepartmentId,max(e.Salary) as Salary,d.Name from Employee as e inner join Department as d on e.DepartmentId =d.Id group by e.DepartmentId) as t on e.DepartmentId=t.DepartmentId and e.Salary=t.Salary;

    +------------+----------+--------+
    | Department | Employee | Salary |
    +------------+----------+--------+
    | Sales | Henry | 80000 |
    | IT | Max | 90000 |
    +------------+----------+--------+
    2 rows in set (0.00 sec)

  • 相关阅读:
    Asp.net使用DevExpress的某些控件不能操作ViewState的解决方案
    关于 vue 循环组件,组件内有根据要求请求select下拉列表,组件内还有自身组件,select下拉列表无法正确获取的问题解决
    Vue+axios请求本地json
    关于vuevideoplayer 实现跳转到特定位置并自动播放
    VueQuillEditor回显不显示空格的处理办法
    elementui 的CascaderPanel级联面板类型 懒加载 回显
    elementui 中的文本域的autosize的意思
    解决 [Element Warn][Form]model is required for validate to work!
    初涉simulink
    arm学习计划
  • 原文地址:https://www.cnblogs.com/hxiaoli/p/7966552.html
Copyright © 2011-2022 走看看