【MySQL】查询语法简介

CodeSpecter
• 阅读 1547

本篇文章主要简介下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_sal
from  emp 
where job='SALESMAN'
group by deptno ,job
having  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不去重
点赞
收藏
评论区
推荐文章
Wesley13 Wesley13
3年前
MySQL UPDATE 查询
MySQLUPDATE查询如果我们需要修改或更新MySQL中的数据,我们可以使用SQLUPDATE命令来操作。.语法以下是UPDATE命令修改MySQL数据表数据的通用SQL语法:UPDATEtable_nameSETfield1newvalue1,field2n
Wesley13 Wesley13
3年前
MySQL千万级别优化·中
MySQL千万级别的查询优化手段·中单列索引(假设在v\_record表中存在id列的索引)1、WHERE条件使用​EXPLAINSELECT\FROMv\_recordWHEREid2​结论:利用索引进行回表查询2、SELECT字段使用
Wesley13 Wesley13
3年前
mysql 数据库group by 按排序取值
mysql数据库groupby按排序取值 mysql5.7以后,groupby想要按照排序的顺序来取值,在子结果集中要有union,groupby等操作,不然按照id默认排序来取值示例:数据库:score表id user\_id num1 100     952 100     963
Wesley13 Wesley13
3年前
MySQL 数据查询语言(DQL)& 事务控制语言(TCL)详解
一、数据查询语言(DQL)(重中之重)完整语法格式:select表达式1|字段,....\from表名where条件\\groupby列名\\having条件\\orderby列名
Wesley13 Wesley13
3年前
MYSQL与TiDB的执行计划
前言这里采用了tpch一个数据库的数据量来进行查询计划的对比。并借助tpch中的22条查询语句进行执行计划分析。mysql采用的是标准安装,TiDB采用的是单机测试版,这里的性能结果不能说明其性能差异本文章主要目的是对比Mysql与TiDB在执行sql查询时的差异。mysql版本5.7  TiDB版本v2.0.0rc.4准备
Wesley13 Wesley13
3年前
MySQL学习——查询表里的数据
MySQL学习——查询表里的数据摘要:本文主要学习了使用DQL语句查询表里数据的方法。数据查询语法1selectdistinct列1as'别名1',...,列nas'别名n'from表名2where表达式3group
Wesley13 Wesley13
3年前
mysql数据库查询操作
\mysql数据库\知识要点:1\.单表查询2\.子查询3\.联表查询4\.事务在进行查询之前,我们要先建好关系表,并往数据表中插入些数据。为查询操作做好准备。\五张关系表的创建:\\\mysql创建并进入数据库:mysqlCREATEDATABASE\
Wesley13 Wesley13
3年前
MySQL数据库出现Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated colum...
原因:MySQL5.7.5及以上功能依赖检测功能。如果启用了ONLY\_FULL\_GROUP\_BYSQL模式(默认情况下),MySQL将拒绝选择列表,HAVING条件或ORDERBY列表的查询引用在GROUPBY子句中既未命名的非集合列,也不在功能上依赖于它们。(5.7.5之前,MySQL没有检测到功能依赖关系,默认情况下不启用O
Wesley13 Wesley13
3年前
Mysql占用过高CPU时的优化手段
慢查询日志,将那些执行时间过长且占用资源过多的SQL拿来进行explain分析,导致CPU过高,多数是GroupBy、OrderBy排序问题所导致,然后慢慢进行优化改进。比如优化insert语句、优化groupby语句、优化orderby语句、优化join语句等等;3)考虑定时优化文件及索引;4)定期分析表,使用optimizetable;
Wesley13 Wesley13
3年前
MYSQL常用查询
一、MYSQL查询的五种子句where(条件查询)、having(筛选)、groupby(分组)、orderby(排序)、limit(限制结果数)【1】where:比较运算符    ,<,,!(<),,<in(param1,p
研发日常踩坑-Mysql分页数据重复 | 京东云技术团队
踩坑描述:写分页查询接口,orderby和limit混用的时候,出现了排序的混乱情况在进行第N页查询时,出现与第一前面页码的数据一样的记录。问题在MySQL中分页查询,我们经常会用limit,如:limit(0,20)表示查询第一页的20条数据,limit