MySQL无重复查询是MySQL中重要的一种查询方式,下面就为您介绍MySQL无重复查询的实现方法,希望对您能有所启迪。
MySQL无重复查询即独有查询,也就是查询满足条件的、但又互不相同的结果。首先让我们看一个例子:
- select title from employee_data;
- +----------------------------+
- | title |
- +----------------------------+
- | CEO |
- | Senior Programmer |
- | Senior Programmer |
- | Web Designer |
- | Web Designer |
- | Programmer |
- | Programmer |
- | Programmer |
- | Programmer |
- | Multimedia Programmer |
- | Multimedia Programmer |
...
上述例子里,我们在employee_data表里查询title(职务),注意到结果里有许多是重复的。如果我们想得到没有重复的结果,就可以用到mysql的关键字 - DISTINCT
- select DISTINCT title from employee_data;
- +----------------------------+
- | title |
- +----------------------------+
- | CEO |
- | Customer Service Manager |
- | Finance Manager |
- | Marketing Executive |
- | Multimedia Programmer |
- | Programmer |
- | Senior Marketing Executive |
- | Senior Programmer |
- | Senior Web Designer |
- | System Administrator |
- | Web Designer |
- +----------------------------+
- 11 rows in set (0.00 sec)
这样就可以把公司的所有职务类别无重复地列出。另外,我们还可以同时使用DISTINCT和ORDBER BY把独有的(无重复的)结果排序列出,例如:
- select DISTINCT age from employee_data
- ORDER BY age;
- +------+
- | age |
- +------+
- | 25 |
- | 26 |
- | 27 |
- | 28 |
- | 30 |
- | 31 |
- | 32 |
- | 33 |
- | 34 |
- | 35 |
- | 36 |
- | 43 |
- +------+
- 12 rows in set (0.00 sec)
【编辑推荐】