C++后端开发(4.1.0)——MySQL学习笔记2distinct、连接查询、子查询、union、limit
1.去重distinct
select distinct job from emp;
+-----------+
| job |
+-----------+
| CLERK |
| SALESMAN |
| MANAGER |
| ANALYST |
| PRESIDENT |
+-----------+
distinct出现在job,deptno两个字段之前,表示两个字段联合起来去重。
select distinct job,deptno from emp;
+-----------+--------+
| job | deptno |
+-----------+--------+
| CLERK | 20 |
| SALESMAN | 30 |
| MANAGER | 20 |
| MANAGER | 30 |
| MANAGER | 10 |
| ANALYST | 20 |
| PRESIDENT | 10 |
| CLERK | 30 |
| CLERK | 10 |
+-----------+--------+
2. 联结表
从一张表中单独查询,称为单表查询。
emp表和dept表联合起来查询数据,从emp表中取员工名字,从dept表中取部门名字。这种跨表查询,多张表联合起来查询数据,被称为连接查询。
2.1 内连接
2.1.1 等值连接
2.1.2 非等值连接
2.1.3 自连接
2.2 外连接
2.1.1 等值连接
2.1.2 非等值连接
2.3 全连接
3.子查询
4. union合并查询结果集
5. limit
6.DQL语句总结
select
...
from
...
where
...
group by
...
having
...
order by
...
limit
...
执行顺序
1.from
2.where
3.group by
4.having
5.select
6.order by
7.limit..