首頁  >  文章  >  資料庫  >  如何才能取得MySQL結果集中某一列的唯一值?

如何才能取得MySQL結果集中某一列的唯一值?

WBOY
WBOY轉載
2023-09-14 15:57:031105瀏覽

如何才能取得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)

以上是如何才能取得MySQL結果集中某一列的唯一值?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除