如何获得 MySQL 结果集中的列的唯一值?
在从 MySQL 表中查询数据时,我们可能会从某个列中获取重复值。借助于 SELECT 语句中的 DISTINCT 子句,我们可以去除结果集中的重复数据。
语法
SELECT DISTINCT Columns FROM Table_name WHERE conditions;
示例
例如,我们有一个名为 'tender' 的表,其中包含以下列 -
mysql> Select * from tender; +----------+--------------+--------------+-------+ | clientid | client_Fname | Client_Lname | value | +----------+--------------+--------------+-------+ | 100 | Mohan | Kumar | 60000 | | 101 | Sohan | Singh | 50000 | | 101 | Somil | Rattan | 55000 | | 103 | Gaurav | Kumar | 75000 | | 103 | Rahul | Singh | 63000 | +----------+--------------+--------------+-------+ 5 rows in set (0.00 sec)
现在,如果我们只想获取名为 'Client_Lname' 的列的唯一值,则查询如下 -
mysql> Select DISTINCT client_Lname from tender; +--------------+ | client_Lname | +--------------+ | Kumar | | Singh | | Rattan | +--------------+ 3 rows in set (0.05 sec)
下面的查询将对一个名为 'client_Fname' 的列执行同样的操作。
mysql> Select DISTINCT client_Fname from tender; +--------------+ | client_Fname | +--------------+ | Mohan | | Sohan | | Somil | | Gaurav | | Rahul | +--------------+ 5 rows in set (0.00 sec)
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP