MySQL中distinct的使用方法
来源:互联网 发布:python读取zip文件 编辑:程序博客网 时间:2024/05/18 02:14
本文的环境是Windows 10,MySQL版本是5.7.12-log
一、基本使用
distinct
一般是用来去除查询结果中的重复记录的,而且这个语句在select
、insert
、delete
和update
中只可以在select
中使用,具体的语法如下:
select distinct expression[,expression...] from tables [where conditions];
这里的expressions可以是多个字段。本文的所有操作都是针对如下示例表的:
CREATE TABLE `NewTable` ( `id` int(11) NOT NULL AUTO_INCREMENT , `name` varchar(30) NULL DEFAULT NULL , `country` varchar(50) NULL DEFAULT NULL , `province` varchar(30) NULL DEFAULT NULL , `city` varchar(30) NULL DEFAULT NULL , PRIMARY KEY (`id`))ENGINE=InnoDB;
1.1 只对一列操作
这种操作是最常见和简单的,如下:
select distinct country from person
结果如下:
1.2 对多列进行操作
select distinct country, province from person
结果如下:
从上例中可以发现,当distinct
应用到多个字段的时候,其应用的范围是其后面的所有字段,而不只是紧挨着它的一个字段,而且distinct
只能放到所有字段的前面,如下语句是错误的:
SELECT country, distinct province from person; // 该语句是错误的
抛出错误如下:
[Err] 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘DISTINCT province from person’ at line 1
1.3 针对NULL
的处理
从1.1和1.2中都可以看出,distinct
对NULL
是不进行过滤的,即返回的结果中是包含NULL
值的。
1.4 与ALL
不能同时使用
默认情况下,查询时返回所有的结果,此时使用的就是all
语句,这是与distinct
相对应的,如下:
select all country, province from person
结果如下:
1.5 与distinctrow
同义
select distinctrow expression[,expression...] from tables [where conditions];
这个语句与distinct的作用是相同的。
1.6 对*
的处理
*
代表整列,使用distinct对*
操作
sql
select DISTINCT * from person
相当于
select DISTINCT id, `name`, country, province, city from person;
阅读全文
0 0
- MySQL中distinct的使用方法
- Mysql中count(*),DISTINCT的使用方法和效率研究
- Mysql中count(*),DISTINCT的使用方法和效率研究
- Mysql中count(*),DISTINCT的使用方法和效率研究
- Mysql中count(*),DISTINCT的使用方法和效率研究
- Mysql中count(*),DISTINCT的使用方法和效率研究
- Mysql中count(*),DISTINCT的使用方法和效率研究
- Mysql中count(*),DISTINCT的使用方法和效率研究
- Mysql中count(*),DISTINCT的使用方法和效率研究
- Mysql中count(*),DISTINCT的使用方法和效率研究
- Mysql中count(*),DISTINCT的使用方法和效率研究
- mysql中count(*),distinct的使用方法和效率研究
- Mysql中count(*),DISTINCT的使用方法和效率研究
- mysql中count(*),distinct的使用方法和效率研究
- Mysql中count(*),DISTINCT的使用方法和效率研究
- Mysql中count(*),DISTINCT的使用方法和效率研究
- MySql中distinct的用法
- MySql中distinct的用法
- 一张图说明HTTPS 握手过程
- 路由器RIP协议配置命令大全
- warning C4996:
- 如何下载今日头条视频
- Qt中如何在点击控件时获取QTreeWidgetItem绑定控件所在的item
- MySQL中distinct的使用方法
- CString 字符串拆分
- 我眼里的清华与姚班
- OC中方法和函数的区别
- 创建Azure scheduler完成日常任务
- Eclipse Bugs
- poj1006 Biorhythms中国剩余定理
- 项目“No bean named 'shiroFilter' is defined”错误处理
- 第一站JavaWeb