Database: sql query data with empty value

In daily data processing, we often encounter situations where we need to query data whose field value is empty.

In this case, we can use SQL statements to query data with empty values ​​in order to better analyze and process the data.

1. Query NULL value

In SQL, NULL represents a missing or unknown value. To query NULL values, you can use the IS NULL keyword.

For example, to query the data whose age field is empty in a certain table, you can use the following statement:

SELECT * FROM table_name WHERE age IS NULL;

Through the above statement, you can query all data whose age field value is empty.

2. Query the empty string value

Sometimes, the value of a certain field is "" (empty string) instead of NULL, and this data also needs to be queried.

You can use the following statement to query data in a table whose name field is an empty string:

SELECT * FROM table_name WHERE name = '';

Through the above statement, you can query all data whose name field value is an empty string.

3. Query the value of blank characters

Whitespace characters refer to spaces, tabs, newlines, etc. in strings; sometimes, you need to query data whose field value only contains whitespace characters.

You can use the following statement to query data whose address field in a table contains only blank characters:

SELECT * FROM table_name WHERE TRIM(address) = '';

Through the above statement, you can query all data whose address field contains only blank characters.

4. Query data with multiple fields whose values ​​are empty

Sometimes, you need to query data with multiple field values ​​that are empty at the same time.

You can use the following statement to query data in a table where both the name and age fields are empty:

SELECT * FROM table_name WHERE name IS NULL AND age IS NULL;

Through the above statement, you can query all data whose name and age field values ​​are both empty.

5. Query data whose uncertain field value is empty

In some cases, the value of a field may or may not be empty.

You can use the following statement to query data in a table for which the phone field value is empty or not empty:

SELECT * FROM table_name WHERE phone IS NULL OR phone = '';

Through the above statement, you can query all data whose phone field value is empty or not empty.

6. Query data whose value is not empty

In addition to querying data whose value is empty, sometimes it is also necessary to query data whose value is not empty.

You can use the following statement to query data in a table whose email field value is not empty:

SELECT * FROM table_name WHERE email IS NOT NULL;

Through the above statement, you can query all data whose email field value is not empty.

To sum up, data with empty SQL query values ​​can be analyzed and queried from multiple angles. Select different query statements as needed to better process and analyze the data.

Guess you like

Origin blog.csdn.net/xun527/article/details/134549611