语法
SELECT DISTINCT * FROM table ORDER BY field1 [ASC|DESC], field2 [ASC|DESC];
GROUP BY
语句用来与COUNT, SUM
等聚合函数联合使用来得到一个或多个列的结果集,也可用于去除重复但效率比DISTINCT
低。
例子
//结构
CREATE TABLE `user` (
`id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`name` varchar(10) NOT NULL,
`sex` tinyint(4) NOT NULL,
`tel` char(11) NOT NULL,
PRIMARY KEY (`id`)
)ENGINE=InnoDB DEFAULT CHARACTER SET utf8;
//去除name字段重复的数据
SELECT * FROM `user` GROUP BY `name`;
//查询sex字段不同值的数量
SELECT COUNT(`sex`),`sex` FROM `user` GROUP BY `sex`;