mysql common syntax finishing

First, create table statement


CREATE TABLE `dept` (
  `deptno` varchar(11) DEFAULT NULL,
  `dname` varchar(255) DEFAULT NULL,
  `loc` varchar(255) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO `test01`.`emp`(`empno`, `ename`, `job`, `mgr`, `sal`, `deptno`) VALUES ('7369', 'smith', 'clerk', '7902', 800, 20);
INSERT INTO `test01`.`emp`(`empno`, `ename`, `job`, `mgr`, `sal`, `deptno`) VALUES ('7499', 'allen', 'salesman', '7698', 1600, 30);
INSERT INTO `test01`.`emp`(`empno`, `ename`, `job`, `mgr`, `sal`, `deptno`) VALUES ('7521', 'ward', 'manage', '7698', 1250, 30);
INSERT INTO `test01`.`emp`(`empno`, `ename`, `job`, `mgr`, `sal`, `deptno`) VALUES ('7566', 'martin', 'salesman', '7839', 2975, 20);
INSERT INTO `test01`.`emp`(`empno`, `ename`, `job`, `mgr`, `sal`, `deptno`) VALUES ('7698', 'blake', 'manage', '7839', 2850, 30);
INSERT INTO `test01`.`emp`(`empno`, `ename`, `job`, `mgr`, `sal`, `deptno`) VALUES ('7788', 'scott', 'analyst', NULL, NULL, 20);
INSERT INTO `test01`.`emp`(`empno`, `ename`, `job`, `mgr`, `sal`, `deptno`) VALUES ('7839', 'king', 'president', NULL, 5000, 10);
INSERT INTO `test01`.`emp`(`empno`, `ename`, `job`, `mgr`, `sal`, `deptno`) VALUES ('7844', 'turner', 'salesman', NULL, NULL, 20);
INSERT INTO `test01`.`emp`(`empno`, `ename`, `job`, `mgr`, `sal`, `deptno`) VALUES ('7876', 'adams', 'clerk', NULL, NULL, 30);
INSERT INTO `test01`.`emp`(`empno`, `ename`, `job`, `mgr`, `sal`, `deptno`) VALUES ('7900', 'james', 'analyst', NULL, NULL, 20);
INSERT INTO `test01`.`emp`(`empno`, `ename`, `job`, `mgr`, `sal`, `deptno`) VALUES ('7902', 'frod', 'analyst', NULL, NULL, 10);

CREATE TABLE `dept` (
  `deptno` varchar(11) DEFAULT NULL,
  `dname` varchar(255) DEFAULT NULL,
  `loc` varchar(255) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `dept` VALUES ('10 ', 'accounting ', 'new york ');
INSERT INTO `dept` VALUES ('20', 'research', 'dallas');
INSERT INTO `dept` VALUES ('30', 'sales  ', 'chicago ');
INSERT INTO `dept` VALUES ('40 ', 'operat ', 'boston ');

Second, the underlying query table

1 Table of the underlying query

the SELECT * from emp;
the SELECT empno "employee number", ename "employee name", job "employee number", mgr "higher number", hiredate "entry date", sal "wages", comm "bonus", deptno "department No "from emp;

2、 distinct

select deptno from emp distinct; eliminate duplicate rows, there are a few statistics department number

3、 count

1.1 calculate the number of employees

select count(*) deptno from emp;

1.2 job by eliminating duplicate rows:

select distinct job from emp;

1.3 by job, deptno eliminate duplicate rows:

select distinct job,deptno from emp;

1.3 how statistics have several jobs in each department.

select count(temp.job),temp.deptno from(select distinct job,deptno from emp) temp group by temp.deptno;

4、where、 like、in、is 、

select * from emp where sal>=2000 and sal<=2500
select ename,sal from emp where ename like 's%';
select ename,sal from emp where ename like '__O%';_表示两个字符。
select * from emp where empno in (7369,7844);
select * from emp where mgr is null ;
select * from emp where (sal>500 or job='manager') and ename like 'J%';

5、order by

select * from emp order by sal asc ; you can also display line. asc default ascending, desc descending order.
ename SELECT, SAL
12 is "salary" from emp order by "salary"; alias SortingUse column (alias required "" No. circle)
SELECT DEPTNO, SAL from EMP Order by DEPTNO, SAL desc; depto ascending press, and then by sal descending order.

6, the clustering function max (maximum), min (minimum), AVG (average), SUM (and), COUNT (count)

Display the minimum wage and shows the employee name:
the SELECT ename "name", sal "wages" from emp where sal = (select min (sal) from emp)
shows the average salary of all employees and pay the sum of:
the SELECT AVG (SAL), SUM (sal) from emp;
the name above the average wage of the employee and his salary is displayed:
the SELECT ename, the WHERE SAL SAL from emp> (the SELECT AVG (SAL) from emp);
display the name of the highest wages of employees, jobs and salaries:
the SELECT ename, the job, the WHERE SAL SAL from emp = (the SELECT max (SAL) from emp);
show wages higher than the average wage of the employee information, and displays the average wage (inefficient):
the SELECT ename, job, sal, (select avg ( sal) from emp) " average salary" from emp where sal> (select avg (sal) from emp);

Second, the complex query tables

  • 1group by a group statistics on the results of inquiries
  • 2having clause restricts the grouped results
  • 3 - group by clause and having (in combination with HAVING group by, can filter the results of the query packet)
  • 4, only grouping function appears in the list, having, order by, a sequence group by, having, order by
  • 5, if the selected columns in the column, expressions, and grouping functions, expressions and then these columns must have a in the group by clause, otherwise an error
    such as: select deptno, avg (sal) , max ( sal) from emp group by deptno having avg (sal) <2000
    where deptno it must be in the group by the

    1. How to display the average wage and maximum salary for each department:

select deptno"部门号",avg(sal)"平均工资",max(sal)"最高工资",min(sal)"最低工资" from emp group by deptno;
+--------+-------------+----------+----------+
| 部门号 | 平均工资    | 最高工资 | 最低工资 |
+--------+-------------+----------+----------+
|     10 | 2916.666667 |  5000.00 |  1300.00 |
|     20 | 2175.000000 |  3000.00 |   800.00 |
|     30 | 1566.666667 |  2850.00 |   950.00 |
+--------+-------------+----------+----------+

2. How to display the average wage and maximum wage for each department and displays the name of the department (multi-table queries):

select emp.deptno "sector number", avg (sal) "average wage", max (sal) "maximum wage", dept.dname "department name" from emp, dept where emp.deptno = dept.deptno group by emp. deptno, dept.dname;

+--------+-------------+----------+------------+
| 部门号 | 平均工资    | 最高工资 | 部门名称   |
+--------+-------------+----------+------------+
|     10 | 2916.666667 |  5000.00 | accounting |
|     20 | 2175.000000 |  3000.00 | research   |
|     30 | 1566.666667 |  2850.00 | sales      |
+--------+-------------+----------+------------+

select emp.deptno "sector number", avg (sal) "average wage", max (sal) "maximum wage", dept.dname "department name" from emp, dept where emp.deptno = dept.deptno group by emp. deptno;

+--------+-------------+----------+------------+
| 部门号 | 平均工资    | 最高工资 | 部门名称   |
+--------+-------------+----------+------------+
|     10 | 2916.666667 |  5000.00 | accounting |
|     20 | 2175.000000 |  3000.00 | research   |
|     30 | 1566.666667 |  2850.00 | sales      |
+--------+-------------+----------+------------+

3. Each job is displayed in each department of the average wage and the minimum wage: sequential group by, having, order by

select deptno "sector number", job "job", avg (sal) "average wage", min (sal) "minimum wage" from emp group by deptno, job order by deptno

+--------+-----------+-------------+----------+
| 部门号 | 岗位      | 平均工资    | 最低工资 |
+--------+-----------+-------------+----------+
|     10 | clerk     | 1300.000000 |  1300.00 |
|     10 | manager   | 2450.000000 |  2450.00 |
|     10 | president | 5000.000000 |  5000.00 |
|     20 | analyst   | 3000.000000 |  3000.00 |
|     20 | clerk     |  950.000000 |   800.00 |
|     20 | manager   | 2975.000000 |  2975.00 |
|     30 | clerk     |  950.000000 |   950.00 |
|     30 | manager   | 2850.000000 |  2850.00 |
|     30 | salesman  | 1400.000000 |  1250.00 |
+--------+-----------+-------------+----------+

select deptno "sector number", job "job", avg (sal) "average wage", min (sal) "minimum wage" from emp group by deptno order by deptno;
there are problems, we must Yaoan department numbers and job points. Group by back behind herein must have, in addition to the function of the cluster field.

4. Display lower than the average salary department number 2000 and its average wage: sequence group by, having, order by

select deptno "sector number", avg (sal) "average salary" from emp group by deptno having avg (sal) <2000;

+--------+-------------+
| 部门号 | 平均工资    |
+--------+-------------+
|     30 | 1566.666667 |
+--------+-------------+

select deptno "sector number", avg (sal) "average salary" from emp group by deptno;

+--------+-------------+
| 部门号 | 平均工资    |
+--------+-------------+
|     10 | 2916.666667 |
|     20 | 2175.000000 |
|     30 | 1566.666667 |
+--------+-------------+

The display is greater than the average salary and department number 2000 of its average wage in ascending order,:

select deptno "sector number", avg (sal) "average salary" from emp group by deptno having avg (sal) <2000

+--------+-------------+----------+----------+
| 部门号 | 平均工资    | 最高工资 | 最低工资 |
+--------+-------------+----------+----------+
|     10 | 2916.666667 |  5000.00 |  1300.00 |
|     20 | 2175.000000 |  3000.00 |   800.00 |
|     30 | 1566.666667 |  2850.00 |   950.00 |
+--------+-------------+----------+----------+

6. Display the department number 20 is the average wage sector

select deptno "sector number", avg (sal) "average salary" from emp group by deptno having deptno = 20;

+--------+-------------+
| 部门号 | 平均工资    |
+--------+-------------+
|     20 | 2175.000000 |
+--------+-------------+

The display is greater than the average salary and department number 2000 of its average wage in ascending order,

select deptno "sector number", avg (sal) "average salary" from emp group by deptno having avg (sal)> 2000 order by "average wage";

+--------+-------------+
| 部门号 | 平均工资    |
+--------+-------------+
|     10 | 2916.666667 |
|     20 | 2175.000000 |
+--------+-------------+

Multi-table query: Note that if more than one table has the same name field, you need to bring the table name (alias)

8. display the name and location of its sales department employees:

select emp.ename "employee name", dept.dname "department name", dept.loc "department location" from emp, dept where dept.dname = 'sales' and emp.deptno = dept.deptno;

+----------+----------+------------+
| 员工姓名 | 部门名称 | 部门所在地 |
+----------+----------+------------+
| allen    | sales    | chicago    |
| ward     | sales    | chicago    |
| martin   | sales    | chicago    |
| blake    | sales    | chicago    |
| turner   | sales    | chicago    |
| james    | sales    | chicago    |
+----------+----------+------------+

9. The name displays the name of the employee, the employee's wages and where the department:

select emp.ename,dept.dname,dept.loc from emp,dept
where dept.dname='sales' and emp.deptno=dept.deptno;

+--------+-------+---------+
| ename  | dname | loc     |
+--------+-------+---------+
| allen  | sales | chicago |
| ward   | sales | chicago |
| martin | sales | chicago |
| blake  | sales | chicago |
| turner | sales | chicago |
| james  | sales | chicago |
+--------+-------+---------+

10. How to Display the Job No. 10 department name, employee name and salary:

select dept.dname,emp.ename,emp.sal from dept,emp where dept.deptno=10 and dept.deptno=emp.deptno;

+------------+--------+---------+
| dname      | ename  | sal     |
+------------+--------+---------+
| accounting | clark  | 2450.00 |
| accounting | king   | 5000.00 |
| accounting | miller | 1300.00 |
+------------+--------+---------+

select dept.dname, emp.ename, emp.sal from dept, emp where emp.deptno = 10 and dept.deptno = emp.deptno; displaying the result is the same.

11. The display employee name, employee wages and where the department name and press department of the sort:

select e.ename "employee name", e.sal "wages", d.dname "department name" from emp e, dept d where e.deptno = d.deptno order by d.dname;

+----------+---------+------------+
| 雇员名字 | 工资    | 部门名称   |
+----------+---------+------------+
| king     | 5000.00 | accounting |
| clark    | 2450.00 | accounting |
| miller   | 1300.00 | accounting |
| smith    |  800.00 | research   |
| ford     | 3000.00 | research   |
| jones    | 2975.00 | research   |
| scott    | 3000.00 | research   |
| adams    | 1100.00 | research   |
| martin   | 1250.00 | sales      |
| james    |  950.00 | sales      |
| ward     | 1250.00 | sales      |
| allen    | 1600.00 | sales      |
| blake    | 2850.00 | sales      |
| turner   | 1500.00 | sales      |
+----------+---------+------------+

Alias field more sense to column name duplication, such as a table when the query field is twice or more times:
the SELECT username AS name, username, the User Email the FROM
or two more tables and queries, have the same return fields. Use an alias.
select emp.ename "employee name", emp.sal "wages", dept.dname "department name" from emp, the dept = dept.deptno the Order by the WHERE emp.deptno dept.dname;
// query results as useless alias

+----------+---------+------------+
| 雇员名字 | 工资    | 部门名称   |
+----------+---------+------------+
| king     | 5000.00 | accounting |
| clark    | 2450.00 | accounting |
| miller   | 1300.00 | accounting |
| smith    |  800.00 | research   |
| ford     | 3000.00 | research   |
| jones    | 2975.00 | research   |
| scott    | 3000.00 | research   |
| adams    | 1100.00 | research   |
| martin   | 1250.00 | sales      |
| james    |  950.00 | sales      |
| ward     | 1250.00 | sales      |
| allen    | 1600.00 | sales      |
| blake    | 2850.00 | sales      |
| turner   | 1500.00 | sales      |
+----------+---------+------------+

12 shows the superiors of an employee's name, such as displaying "ford" superiors:

select (select ename from emp where ename='ford')"员工姓名",ename"上级领导" from emp
where empno=(select mgr from emp where ename='ford');

+----------+----------+
| 员工姓名 | 上级领导 |
+----------+----------+
| ford     | jones    |
+----------+----------+

Third, the table join query

1 shows the company each employee's name and the name of his superiors - the analysis of the emp table as two tables are the worker / outside boss-- connection (left outer join, right outer join)

select worker.ename "employee name", boss.ename "leading name" from emp worker, emp boss where worker.mgr = boss.empno;

 +----------+----------+
| 员工名字 | 领导名字 |
+----------+----------+
| smith    | ford     |
| allen    | blake    |
| ward     | blake    |
| jones    | king     |
| martin   | blake    |
| blake    | king     |
| clark    | king     |
| scott    | jones    |
| turner   | blake    |
| adams    | scott    |
| james    | blake    |
| ford     | jones    |
| miller   | clark    |
+----------+----------+

Complex queries IV table

1, - sub-queries, and how to display all employees in the same department smith:

select deptno"部门号",ename"员工名字" from emp where deptno=(select deptno from emp where ename='smith');

+--------+----------+
| 部门号 | 员工名字 |
+--------+----------+
|     20 | smith    |
|     20 | jones    |
|     20 | scott    |
|     20 | adams    |
|     20 | ford     |
+--------+----------+

2, how to query the same work and 10 departments of the employee's name, position, salary, department number

The first step: select distinct job from emp where deptno = 10; the department number 10 for the work to eliminate duplication.

+-----------+
| job       |
+-----------+
| manager   |
| president |
| clerk     |
+-----------+

第二步:select * from emp where job in (select distinct job from emp where deptno=10);

+-------+--------+-----------+------+---------------------+---------+------+--------+
| empno | ename  | job       | mgr  | hiredate            | sal     | comm | deptno |
+-------+--------+-----------+------+---------------------+---------+------+--------+
|  7369 | smith  | clerk     | 7902 | 1980-12-17 00:00:00 |  800.00 | NULL |     20 |
|  7566 | jones  | manager   | 7839 | 1981-04-02 00:00:00 | 2975.00 | NULL |     20 |
|  7698 | blake  | manager   | 7839 | 1981-05-01 00:00:00 | 2850.00 | NULL |     30 |
|  7782 | clark  | manager   | 7839 | 1981-06-09 00:00:00 | 2450.00 | NULL |     10 |
|  7839 | king   | president | NULL | 1981-11-17 00:00:00 | 5000.00 | NULL |     10 |
|  7876 | adams  | clerk     | 7788 | 1987-05-23 00:00:00 | 1100.00 | NULL |     20 |
|  7900 | james  | clerk     | 7698 | 1981-12-03 00:00:00 |  950.00 | NULL |     30 |
|  7934 | miller | clerk     | 7782 | 1982-01-23 00:00:00 | 1300.00 | NULL |     10 |
+-------+--------+-----------+------+---------------------+---------+------+--------+

3, how to display the average wage is higher than the sector average wage of the employees name, salary, department:

  • One must first know the average salary of each department; select avg (sal) "sector average wage", deptno from emp group by deptno
  • 2 above the query results as a temporary treatment table, there must field alias. Using field aliases: the FROM name the SELECT username user"username" is the original field name, the "name" is an alias
  • 3 used in the from clause subquery It is noted when using a subquery in the from clause, the sub-query is treated as a temporary table, when using a subquery in the from clause, essential to the child query specifies aliases.
    select e.ename "employee name", e.sal "salary", temp.myavg "sector average wage", e.deptno from emp e, ( select avg (sal) myavg, deptno from emp group by deptno) temp where e .deptno = temp.deptno and e.sal> temp.myavg
+----------+---------+--------------+--------+
| 员工名字 | 薪水    | 部门平均工资 | deptno |
+----------+---------+--------------+--------+
| allen    | 1600.00 |  1566.666667 |     30 |
| jones    | 2975.00 |  2175.000000 |     20 |
| blake    | 2850.00 |  1566.666667 |     30 |
| scott    | 3000.00 |  2175.000000 |     20 |
| king     | 5000.00 |  2916.666667 |     10 |
| ford     | 3000.00 |  2175.000000 |     20 |
+----------+---------+--------------+--------+

Taken by the employee id number ascending

  • 1select * from tablename order by orderfield desc/asc limit position, counter;
  • 2SELECT * FROM table LIMIT 5,10; // retrieve rows 6-15, note, as an offset 10
  • 3 to retrieve all rows offset from one end to the set record, and can specify a second parameter to -1:
    the SELECT * the LIMIT the FROM Table 95, -1; // retrieve rows 96-last.
  • 4, given only a parameter that indicates the maximum number of rows returned:
    MySQL> Table the LIMIT the SELECT * the FROM. 5; 5 before retrieving rows // // That is, LIMIT n is equivalent to LIMIT 0, n.
    If you want to get the last several more months of data can order by id desc

    4, show the fifth to the 10th entry of employee information (in chronological order to find recruits)

    1, a first to a fourth entry in the employee (the number indicates to remove the top few records):
select * from emp order by hiredate ;
+-------+--------+-----------+------+---------------------+---------+---------+--------+
| empno | ename  | job       | mgr  | hiredate            | sal     | comm    | deptno |
+-------+--------+-----------+------+---------------------+---------+---------+--------+
|  7369 | smith  | clerk     | 7902 | 1980-12-17 00:00:00 |  800.00 |    NULL |     20 |
|  7499 | allen  | salesman  | 7698 | 1981-02-20 00:00:00 | 1600.00 |  300.00 |     30 |
|  7521 | ward   | salesman  | 7698 | 1981-02-22 00:00:00 | 1250.00 |  500.00 |     30 |
|  7566 | jones  | manager   | 7839 | 1981-04-02 00:00:00 | 2975.00 |    NULL |     20 |
|  7698 | blake  | manager   | 7839 | 1981-05-01 00:00:00 | 2850.00 |    NULL |     30 |
|  7782 | clark  | manager   | 7839 | 1981-06-09 00:00:00 | 2450.00 |    NULL |     10 |
|  7844 | turner | salesman  | 7698 | 1981-09-08 00:00:00 | 1500.00 |    0.00 |     30 |
|  7654 | martin | salesman  | 7698 | 1981-09-28 00:00:00 | 1250.00 | 1400.00 |     30 |
|  7839 | king   | president | NULL | 1981-11-17 00:00:00 | 5000.00 |    NULL |     10 |
|  7900 | james  | clerk     | 7698 | 1981-12-03 00:00:00 |  950.00 |    NULL |     30 |
|  7902 | ford   | analyst   | 7566 | 1981-12-03 00:00:00 | 3000.00 |    NULL |     20 |
|  7934 | miller | clerk     | 7782 | 1982-01-23 00:00:00 | 1300.00 |    NULL |     10 |
|  7788 | scott  | analyst   | 7566 | 1987-04-19 00:00:00 | 3000.00 |    NULL |     20 |
|  7876 | adams  | clerk     | 7788 | 1987-05-23 00:00:00 | 1100.00 |    NULL |     20 |
+-------+--------+-----------+------+---------------------+---------+---------+--------+
select * from emp order by hiredate limit 0,4;
+-------+--------+---------+------+---------------------+---------+------+--------+
| empno | ename  | job     | mgr  | hiredate            | sal     | comm | deptno |
+-------+--------+---------+------+---------------------+---------+------+--------+
|  7902 | ford   | analyst | 7566 | 1981-12-03 00:00:00 | 3000.00 | NULL |     20 |
|  7934 | miller | clerk   | 7782 | 1982-01-23 00:00:00 | 1300.00 | NULL |     10 |
|  7788 | scott  | analyst | 7566 | 1987-04-19 00:00:00 | 3000.00 | NULL |     20 |
+-------+--------+---------+------+---------------------+---------+------+--------+

5、select top 6 * from emp where empno not in(select top 4 empno from emp order by hiredate) order by hiredate

- display the information of the employee 11-13 recruits: 10 is the initial position, 3 for the offset.
select * from emp order by hiredate limit 10,3;

- Show me the first 5-9 entry of employee information (sorted by salary level)
the SELECT * from emp the Order by SAL desc limit 4, 4;

+-------+--------+----------+------+---------------------+---------+--------+--------+
| empno | ename  | job      | mgr  | hiredate            | sal     | comm   | deptno |
+-------+--------+----------+------+---------------------+---------+--------+--------+
|  7698 | blake  | manager  | 7839 | 1981-05-01 00:00:00 | 2850.00 |   NULL |     30 |
|  7782 | clark  | manager  | 7839 | 1981-06-09 00:00:00 | 2450.00 |   NULL |     10 |
|  7499 | allen  | salesman | 7698 | 1981-02-20 00:00:00 | 1600.00 | 300.00 |     30 |
|  7844 | turner | salesman | 7698 | 1981-09-08 00:00:00 | 1500.00 |   0.00 |     30 |
+-------+--------+----------+------+---------------------+---------+--------+--------+

6, show the company name of each employee and his superiors, superiors should not be displayed

- left outer join: If you refer to the table on the left displays all records, if there is no matching record, then fill in with null
the SELECT worker.ename "employee name", boss.ename "leading name" from emp worker left join emp boss on worker .mgr = boss.empno;

+----------+----------+
| 员工名字 | 领导名字 |
+----------+----------+
| smith    | ford     |
| allen    | blake    |
| ward     | blake    |
| jones    | king     |
| martin   | blake    |
| blake    | king     |
| clark    | king     |
| scott    | jones    |
| king     | NULL     |
| turner   | blake    |
| adams    | scott    |
| james    | blake    |
| ford     | jones    |
| miller   | clark    |
+----------+----------+

7, - right outer join: the recording means if the table on the right shows all, if there is no matching record, it is filled with null

select worker.ename "employee name", boss.ename "leading name" from emp worker right join emp boss on worker.mgr = boss.empno;

+----------+----------+
| 员工名字 | 领导名字 |
+----------+----------+
| smith    | ford     |
| allen    | blake    |
| ward     | blake    |
| jones    | king     |
| martin   | blake    |
| blake    | king     |
| clark    | king     |
| scott    | jones    |
| turner   | blake    |
| adams    | scott    |
| james    | blake    |
| ford     | jones    |
| miller   | clark    |
| NULL     | smith    |
| NULL     | allen    |
| NULL     | ward     |
| NULL     | martin   |
| NULL     | turner   |
| NULL     | adams    |
| NULL     | james    |
| NULL     | miller   |
+----------+----------+

Guess you like

Origin www.cnblogs.com/zhangke306shdx/p/11084541.html