Home > Database > Mysql Tutorial > body text

What does MySQL return when we use DISTINCT clause on a column with multiple NULL values?

PHPz
Release: 2023-08-28 21:37:08
forward
661 people have browsed it

当我们对具有多个 NULL 值的列使用 DISTINCT 子句时,MySQL 返回什么?

When we use the DISTINCT clause on a column with multiple NULL values, MySQL will treat all NULL values ​​as the same.

This means that based on the properties of the DISTINCT clause, MySQL will return only one NULL value in the result set and eliminate other NULL values. Consider the example of a table named "testing" where there are multiple NULL values ​​in the column "Lname".

mysql> Select * from testing;
+------+---------+---------+
| id   | fname   | Lname   |
+------+---------+---------+
|  200 | Raman   | Kumar   |
|  201 | Sahil   | Bhalla  |
|  202 | Gaurav  | NULL    |
|  203 | Aarav   | NULL    |
|  204 | Harshit | Khurana |
|  205 | Rahul   | NULL    |
|  206 | Piyush  | Kohli   |
|  207 | Lovkesh | NULL    |
+-----+---------+----------+
8 rows in set (0.00 sec)

mysql> SELECT DISTINCT Lname from testing;
+---------+
| Lname   |
+---------+
| Kumar   |
| Bhalla  |
| NULL    |
| Khurana |
| Kohli   |
+---------+
5 rows in set (0.00 sec)
Copy after login

As can be seen from the above query, when we use the DISTINCT clause on the column 'Lname' which has four NULL values, MySQL returns only one NULL and eliminates the other values.

The above is the detailed content of What does MySQL return when we use DISTINCT clause on a column with multiple NULL values?. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!