Home > Database > Mysql Tutorial > body text

How can I get the unique value of a column in a MySQL result set?

WBOY
Release: 2023-09-14 15:57:03
forward
1124 people have browsed it

How can I get the unique value of a column in a MySQL result set?

While querying data from a MySQL table, we may get duplicate values ​​from columns. With the help of the DISTINCT clause in the SELECT statement, we can remove duplicate data from the result set.

Syntax
SELECT DISTINCT Columns FROM Table_name WHERE conditions;
Copy after login

Example

For example, we have a table named "tender" which contains the following columns -

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)
Copy after login

Now if we want to get only the unique values ​​of the column named "Client_Lname" then the following will be the query -

mysql> Select DISTINCT client_Lname from tender;
+--------------+
| client_Lname |
+--------------+
| Kumar        |
| Singh        |
| Rattan       |
+--------------+
3 rows in set (0.05 sec)
Copy after login

The following query will get the unique values ​​of the column named "client_Fname" Columns perform the same operation.

mysql> Select DISTINCT client_Fname from tender;
+--------------+
| client_Fname |
+--------------+
| Mohan        |
| Sohan        |
| Somil        |
| Gaurav       |
| Rahul        |
+--------------+
5 rows in set (0.00 sec)
Copy after login

The above is the detailed content of How can I get the unique value of a column in a MySQL result set?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template