MySQL查询语法汇总

前言:

本篇文章主要简介下MySQL中where,group by ,order by ,limit,join,union ,union all,子表等查询语法。

测试数据准备

create table emp (  empno numeric(4) not null,  ename varchar(10),  job varchar(9),  mgr numeric(4),  hiredate datetime,  sal numeric(7, 2),  comm numeric(7, 2),  deptno numeric(2));
create table dept (  deptno numeric(2),  dname varchar(14),  loc varchar(13));
create table salgrade (  grade numeric,  losal numeric,  hisal numeric);
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, 'OPERATIONS', 'BOSTON');
insert into salgrade values (1, 700, 1200);insert into salgrade values (2, 1201, 1400);insert into salgrade values (3, 1401, 2000);insert into salgrade values (4, 2001, 3000);insert into salgrade values (5, 3001, 9999);
insert into emp values (7369, 'SMITH', 'CLERK', 7902, '1980-12-17', 800, null, 20);insert into emp values (7499, 'ALLEN', 'SALESMAN', 7698, '1981-02-20', 1600, 300, 30);insert into emp values (7521, 'WARD', 'SALESMAN', 7698, '1981-02-22', 1250, 500, 30);insert into emp values (7566, 'JONES', 'MANAGER', 7839, '1981-04-02', 2975, null, 20);insert into emp values (7654, 'MARTIN', 'SALESMAN', 7698, '1981-09-28', 1250, 1400, 30);insert into emp values (7698, 'BLAKE', 'MANAGER', 7839, '1981-05-01', 2850, null, 30);insert into emp values (7782, 'CLARK', 'MANAGER', 7839, '1981-06-09', 2450, null, 10);insert into emp values (7788, 'SCOTT', 'ANALYST', 7566, '1982-12-09', 3000, null, 20);insert into emp values (7839, 'KING', 'PRESIDENT', null, '1981-11-17', 5000, null, 10);insert into emp values (7844, 'TURNER', 'SALESMAN', 7698, '1981-09-08', 1500, 0, 30);insert into emp values (7876, 'ADAMS', 'CLERK', 7788, '1983-01-12', 1100, null, 20);insert into emp values (7900, 'JAMES', 'CLERK', 7698, '1981-12-03', 950, null, 30);insert into emp values (7902, 'FORD', 'ANALYST', 7566, '1981-12-03', 3000, null, 20);insert into emp values (7934, 'MILLER', 'CLERK', 7782, '1982-01-23', 1300, null, 10);

1.模糊查询

mysql> select * from emp where ename like '%S%'; +-------+-------+---------+------+---------------------+---------+------+--------+| 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 || 7788 | SCOTT | ANALYST | 7566 | 1982-12-09 00:00:00 | 3000.00 | NULL |   20 || 7876 | ADAMS | CLERK  | 7788 | 1983-01-12 00:00:00 | 1100.00 | NULL |   20 || 7900 | JAMES | CLERK  | 7698 | 1981-12-03 00:00:00 | 950.00 | NULL |   30 |+-------+-------+---------+------+---------------------+---------+------+--------+5 rows in set (0.00 sec)
mysql> select * from emp where ename like 'S%'; +-------+-------+---------+------+---------------------+---------+------+--------+| empno | ename | job   | mgr | hiredate      | sal   | comm | deptno |+-------+-------+---------+------+---------------------+---------+------+--------+| 7369 | SMITH | CLERK  | 7902 | 1980-12-17 00:00:00 | 800.00 | NULL |   20 || 7788 | SCOTT | ANALYST | 7566 | 1982-12-09 00:00:00 | 3000.00 | NULL |   20 |+-------+-------+---------+------+---------------------+---------+------+--------+2 rows in set (0.01 sec)
mysql> select * from emp where ename like '%S';+-------+-------+---------+------+---------------------+---------+------+--------+| empno | ename | job   | mgr | hiredate      | sal   | comm | deptno |+-------+-------+---------+------+---------------------+---------+------+--------+| 7566 | JONES | MANAGER | 7839 | 1981-04-02 00:00:00 | 2975.00 | NULL |   20 || 7876 | ADAMS | CLERK  | 7788 | 1983-01-12 00:00:00 | 1100.00 | NULL |   20 || 7900 | JAMES | CLERK  | 7698 | 1981-12-03 00:00:00 | 950.00 | NULL |   30 |+-------+-------+---------+------+---------------------+---------+------+--------+3 rows in set (0.00 sec)
mysql> select * from emp where ename like '_O%';+-------+-------+---------+------+---------------------+---------+------+--------+| empno | ename | job   | mgr | hiredate      | sal   | comm | deptno |+-------+-------+---------+------+---------------------+---------+------+--------+| 7566 | JONES | MANAGER | 7839 | 1981-04-02 00:00:00 | 2975.00 | NULL |   20 || 7902 | FORD | ANALYST | 7566 | 1981-12-03 00:00:00 | 3000.00 | NULL |   20 |+-------+-------+---------+------+---------------------+---------+------+--------+2 rows in set (0.00 sec)

总结:%表示任意0个或多个字符,可匹配任意类型和长度的字符;_表示任意单个字符,匹配单个任意字符。

MySQL查询语法汇总

扫一扫手机访问