欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

MySQL查询语法汇总

程序员文章站 2022-04-05 13:16:42
前言:本篇文章主要简介下mysql中where,group by ,order by ,limit,join,union ,union all,子表等查询语法。测试数据准备create table e...

前言:

本篇文章主要简介下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个或多个字符,可匹配任意类型和长度的字符;_表示任意单个字符,匹配单个任意字符。

2.排序

mysql> select * from emp order by sal;+-------+--------+-----------+------+---------------------+---------+---------+--------+| empno | ename | job    | mgr | hiredate      | sal   | comm  | deptno |+-------+--------+-----------+------+---------------------+---------+---------+--------+| 7369 | smith | clerk   | 7902 | 1980-12-17 00:00:00 | 800.00 |  null |   20 || 7900 | james | clerk   | 7698 | 1981-12-03 00:00:00 | 950.00 |  null |   30 || 7876 | adams | clerk   | 7788 | 1983-01-12 00:00:00 | 1100.00 |  null |   20 || 7521 | ward  | salesman | 7698 | 1981-02-22 00:00:00 | 1250.00 | 500.00 |   30 || 7654 | martin | salesman | 7698 | 1981-09-28 00:00:00 | 1250.00 | 1400.00 |   30 || 7934 | miller | clerk   | 7782 | 1982-01-23 00:00:00 | 1300.00 |  null |   10 || 7844 | turner | salesman | 7698 | 1981-09-08 00:00:00 | 1500.00 |  0.00 |   30 || 7499 | allen | salesman | 7698 | 1981-02-20 00:00:00 | 1600.00 | 300.00 |   30 || 7782 | clark | manager  | 7839 | 1981-06-09 00:00:00 | 2450.00 |  null |   10 || 7698 | blake | manager  | 7839 | 1981-05-01 00:00:00 | 2850.00 |  null |   30 || 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 || 7902 | ford  | analyst  | 7566 | 1981-12-03 00:00:00 | 3000.00 |  null |   20 || 7839 | king  | president | null | 1981-11-17 00:00:00 | 5000.00 |  null |   10 |+-------+--------+-----------+------+---------------------+---------+---------+--------+14 rows in set (0.00 sec)
mysql> select * from emp order by sal asc;+-------+--------+-----------+------+---------------------+---------+---------+--------+| empno | ename | job    | mgr | hiredate      | sal   | comm  | deptno |+-------+--------+-----------+------+---------------------+---------+---------+--------+| 7369 | smith | clerk   | 7902 | 1980-12-17 00:00:00 | 800.00 |  null |   20 || 7900 | james | clerk   | 7698 | 1981-12-03 00:00:00 | 950.00 |  null |   30 || 7876 | adams | clerk   | 7788 | 1983-01-12 00:00:00 | 1100.00 |  null |   20 || 7521 | ward  | salesman | 7698 | 1981-02-22 00:00:00 | 1250.00 | 500.00 |   30 || 7654 | martin | salesman | 7698 | 1981-09-28 00:00:00 | 1250.00 | 1400.00 |   30 || 7934 | miller | clerk   | 7782 | 1982-01-23 00:00:00 | 1300.00 |  null |   10 || 7844 | turner | salesman | 7698 | 1981-09-08 00:00:00 | 1500.00 |  0.00 |   30 || 7499 | allen | salesman | 7698 | 1981-02-20 00:00:00 | 1600.00 | 300.00 |   30 || 7782 | clark | manager  | 7839 | 1981-06-09 00:00:00 | 2450.00 |  null |   10 || 7698 | blake | manager  | 7839 | 1981-05-01 00:00:00 | 2850.00 |  null |   30 || 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 || 7902 | ford  | analyst  | 7566 | 1981-12-03 00:00:00 | 3000.00 |  null |   20 || 7839 | king  | president | null | 1981-11-17 00:00:00 | 5000.00 |  null |   10 |+-------+--------+-----------+------+---------------------+---------+---------+--------+14 rows in set (0.00 sec)
mysql> select * from emp order by sal desc;+-------+--------+-----------+------+---------------------+---------+---------+--------+| empno | ename | job    | mgr | hiredate      | sal   | comm  | deptno |+-------+--------+-----------+------+---------------------+---------+---------+--------+| 7839 | king  | president | null | 1981-11-17 00:00:00 | 5000.00 |  null |   10 || 7788 | scott | analyst  | 7566 | 1982-12-09 00:00:00 | 3000.00 |  null |   20 || 7902 | ford  | analyst  | 7566 | 1981-12-03 00:00:00 | 3000.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 || 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 || 7934 | miller | clerk   | 7782 | 1982-01-23 00:00:00 | 1300.00 |  null |   10 || 7521 | ward  | salesman | 7698 | 1981-02-22 00:00:00 | 1250.00 | 500.00 |   30 || 7654 | martin | salesman | 7698 | 1981-09-28 00:00:00 | 1250.00 | 1400.00 |   30 || 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 || 7369 | smith | clerk   | 7902 | 1980-12-17 00:00:00 | 800.00 |  null |   20 |+-------+--------+-----------+------+---------------------+---------+---------+--------+14 rows in set (0.00 sec)

总结:order by排序默认按asc升序来排列也可指定desc降序排列

3.限制多少行

mysql> select * from emp limit 3;+-------+-------+----------+------+---------------------+---------+--------+--------+| 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 |+-------+-------+----------+------+---------------------+---------+--------+--------+3 rows in set (0.00 sec)
mysql> select * from emp order by sal desc limit 3;+-------+-------+-----------+------+---------------------+---------+------+--------+| empno | ename | job    | mgr | hiredate      | sal   | comm | deptno |+-------+-------+-----------+------+---------------------+---------+------+--------+| 7839 | king | president | null | 1981-11-17 00:00:00 | 5000.00 | null |   10 || 7902 | ford | analyst  | 7566 | 1981-12-03 00:00:00 | 3000.00 | null |   20 || 7788 | scott | analyst  | 7566 | 1982-12-09 00:00:00 | 3000.00 | null |   20 |+-------+-------+-----------+------+---------------------+---------+------+--------+3 rows in set (0.00 sec)

总结:limit限定显示前多少行,可与order by联合使用

4.聚合函

count() sum()函数用法:#1.各个部门的薪水和mysql> select deptno,sum(sal) from emp group by deptno;+--------+----------+| deptno | sum(sal) |+--------+----------+|   10 | 8750.00 ||   20 | 10875.00 ||   30 | 9400.00 |+--------+----------+3 rows in set (0.01 sec)
#2.group by字段必须出现在select字段后面  各个部门的各个岗位的薪水和mysql> select deptno,job, sum(sal) from emp group by deptno ,job;+--------+-----------+----------+| deptno | job    | sum(sal) |+--------+-----------+----------+|   10 | clerk   | 1300.00 ||   10 | manager  | 2450.00 ||   10 | president | 5000.00 ||   20 | analyst  | 6000.00 ||   20 | clerk   | 1900.00 ||   20 | manager  | 2975.00 ||   30 | clerk   |  950.00 ||   30 | manager  | 2850.00 ||   30 | salesman | 5600.00 |+--------+-----------+----------+9 rows in set (0.01 sec)
#3.having  薪水和>5000的各个部门的各个岗位mysql> select deptno,job, sum(sal)   -> from emp   -> group by deptno ,job  -> having sum(sal)>5000; +--------+----------+----------+| deptno | job   | sum(sal) |+--------+----------+----------+|   20 | analyst | 6000.00 ||   30 | salesman | 5600.00 |+--------+----------+----------+2 rows in set (0.00 sec)#4.常用组合where order limit select deptno,job, sum(sal) as sum_salfrom emp where job='salesman'group by deptno ,jobhaving sum(sal)>5000 order by sum(sal) desc limit 1;

下面介绍下join及union的用法
数据准备:

create table testa(aid int,aname varchar(40));
create table testb(bid int,bname varchar(40),age int);
insert into testa values(1,'xiaoming');insert into testa values(2,'ly');insert into testa values(3,'kun');insert into testa values(4,'zidong');insert into testa values(5,'hb');

insert into testb values(1,'xiaoming',10);insert into testb values(2,'ly',100);insert into testb values(3,'kun',200);insert into testb values(4,'zidong',110);insert into testb values(6,'niu',120);insert into testb values(7,'meng',130);insert into testb values(8,'mi',170);

5.left join

mysql> select  -> a.aid,a.aname,  -> b.bid,b.bname,b.age  -> from testa as a  -> left join testb as b on a.aid=b.bid; +------+----------+------+----------+------+| aid | aname  | bid | bname  | age |+------+----------+------+----------+------+|  1 | xiaoming |  1 | xiaoming |  10 ||  2 | ly    |  2 | ly    | 100 ||  3 | kun   |  3 | kun   | 200 ||  4 | zidong  |  4 | zidong  | 110 ||  5 | hb    | null | null   | null |+------+----------+------+----------+------+5 rows in set (0.00 sec)

总结:a left join b  a表全,用b表去匹配a表left join 关键字会从左表 (a) 那里返回所有的行,即使在右表 (b) 中没有匹配的行,匹配不到的列用null代替

6.right join

mysql> select  -> a.aid,a.aname,  -> b.bid,b.bname,b.age  -> from testa as a  -> right join testb as b on a.aid=b.bid;+------+----------+------+----------+------+| aid | aname  | bid | bname  | age |+------+----------+------+----------+------+|  1 | xiaoming |  1 | xiaoming |  10 ||  2 | ly    |  2 | ly    | 100 ||  3 | kun   |  3 | kun   | 200 ||  4 | zidong  |  4 | zidong  | 110 || null | null   |  6 | niu   | 120 || null | null   |  7 | meng   | 130 || null | null   |  8 | mi    | 170 |+------+----------+------+----------+------+7 rows in set (0.00 sec)

总结:a right join b  b表全,用a表去匹配b表right join 关键字会右表 (b) 那里返回所有的行,即使在左表 (a) 中没有匹配的行,匹配不到的列用null代替

7.inner join

mysql> select  -> a.aid,a.aname,  -> b.bid,b.bname,b.age  -> from testa as a  -> inner join testb as b on a.aid=b.bid; +------+----------+------+----------+------+| aid | aname  | bid | bname  | age |+------+----------+------+----------+------+|  1 | xiaoming |  1 | xiaoming |  10 ||  2 | ly    |  2 | ly    | 100 ||  3 | kun   |  3 | kun   | 200 ||  4 | zidong  |  4 | zidong  | 110 |+------+----------+------+----------+------+4 rows in set (0.00 sec)
mysql> select  -> a.aid,a.aname,  -> b.bid,b.bname,b.age  -> from testa as a  -> join testb as b on a.aid=b.bid; +------+----------+------+----------+------+| aid | aname  | bid | bname  | age |+------+----------+------+----------+------+|  1 | xiaoming |  1 | xiaoming |  10 ||  2 | ly    |  2 | ly    | 100 ||  3 | kun   |  3 | kun   | 200 ||  4 | zidong  |  4 | zidong  | 110 |+------+----------+------+----------+------+4 rows in set (0.00 sec)

总结:inner join 与join 效果一样在表中存在至少一个匹配时,inner join 关键字返回行

8.union与union all

mysql> select aid,aname from testa  -> union  -> select bid,bname from testb;+------+----------+| aid | aname  |+------+----------+|  1 | xiaoming ||  2 | ly    ||  3 | kun   ||  4 | zidong  ||  5 | hb    ||  6 | niu   ||  7 | meng   ||  8 | mi    |+------+----------+8 rows in set (0.01 sec)
mysql> select aid,aname from testa  -> union all  -> select bid,bname from testb;+------+----------+| aid | aname  |+------+----------+|  1 | xiaoming ||  2 | ly    ||  3 | kun   ||  4 | zidong  ||  5 | hb    ||  1 | xiaoming ||  2 | ly    ||  3 | kun   ||  4 | zidong  ||  6 | niu   ||  7 | meng   ||  8 | mi    |+------+----------+12 rows in set (0.00 sec)

总结:union 会去重 union all不去重

以上就是mysql查询语法汇总的详细内容,更多关于mysql查询语法的资料请关注其它相关文章!