Home > Database > Mysql Tutorial > body text

Detailed explanation and usage introduction of MySQL ISNULL function

王林
Release: 2024-03-01 17:24:03
Original
1352 people have browsed it

MySQL ISNULL 函数详解及用法介绍

The ISNULL() function in MySQL is a function used to determine whether a specified expression or column is NULL. It returns a Boolean value, 1 if the expression is NULL, 0 otherwise. The ISNULL() function can be used in the SELECT statement or for conditional judgment in the WHERE clause.

1. The basic syntax of the ISNULL() function:

ISNULL(expression)
Copy after login

Among them, expression is the expression or column to be judged whether it is NULL.

2. Common situations of using the ISNULL() function:

Example 1: Using the ISNULL() function in a SELECT statement

Suppose there is a student table named students, which contains three columns: id, name and age. If we want to query all student records with NULL age, we can use the following SQL statement:

SELECT * FROM students WHERE ISNULL(age);
Copy after login

The ISNULL(age) in the SQL statement will determine whether the age column is NULL. If it is NULL, the corresponding record will be returned.

Example 2: Update a record with a column value of NULL

If we want to change the age of a record whose age column is NULL to 18 years old, we can use the following UPDATE Statement:

UPDATE students SET age = IF(ISNULL(age), 18, age);
Copy after login

The UPDATE statement uses the ISNULL() function to determine whether the age column is NULL. If it is NULL, set the age to 18 years old, otherwise keep the original value.

3. Advanced usage of the ISNULL() function:

Example 3: Using the ISNULL() function for table joins

Suppose there are two tables students and scores, which store student information and score information respectively. If we want to query all students whose age is NULL and their corresponding score records, we can use the following SQL statement:

SELECT s.id, s.name, sc.score
FROM students s
LEFT JOIN scores sc ON s.id = sc.student_id
WHERE ISNULL(s.age);
Copy after login

The The SQL statement uses the ISNULL() function to judge the age column in the student table, thereby filtering out student records with NULL age, and connects the student table and the score table through LEFT JOIN.

Through the above examples, we can see the flexible application of the ISNULL() function in MySQL. Whether you are filtering records in a SELECT statement, updating data in an UPDATE statement, or making conditional judgments when connecting tables, the ISNULL() function can provide great help. I hope the introduction in this article can help readers better understand the usage of the ISNULL() function in MySQL.

The above is the detailed content of Detailed explanation and usage introduction of MySQL ISNULL function. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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!