Problem with mysql null value query not coming out

Recently encountered the problem of mysql null value query. When querying this field, some of the fields are null and some are not null. If the query condition of this field is query, the value of null will not be displayed.

Example

The default values ​​of the new table test_user name and phone are null.

 Let's add some data

 Check if the name is not Zhang Sande’s data

select * from test_user where name !='张三'

 Why can't it be found if the ID is 4? If your business name is null, the value must also be found out, which will cause data loss.

The same query shows that the mobile phone number is not 15822222222.

SELECT * FROM test_user WHERE phone !='15822222222'

 Wang Er is missing

Let’s try changing the default values ​​of name and phone to empty.

Use SQL statements to change null fields in the database to empty

UPDATE test_user SET NAME = IFNULL(NAME,'')

UPDATE test_user SET phone = IFNULL(phone,'')

Let’s query the above sql again

The empty value can be found.

 When creating a table, you should pay attention to whether the default value of the created field is empty or null according to your own business scenario.

 

Guess you like

Origin blog.csdn.net/weixin_41018853/article/details/131940142