Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selecting rows with null in integer columns using mysql query

Tags:

mysql

I have a table named 'datatablecoulmn' with the following columns. enter image description here

now i want all rows where the column FkID is NULL.FkID is an integer field

i tried the following queries

             SELECT * FROM `datatablecoulmn` WHERE `FkID`=NULL
             SELECT * FROM `datatablecoulmn` WHERE `FkID`<1
             SELECT * FROM `datatablecoulmn` WHERE `FkID`='null'

All of these returns empty rows .Any help?

like image 555
Sherin Avatar asked Dec 04 '22 04:12

Sherin


1 Answers

In MySQL, NULL is considered as a 'missing, unknown value', as opposed to no value. Any arithmetic comparison with NULL does not return true or false, but returns NULL instead., So, NULL != 'C' returns NULL, as opposed to returning true.

Use IS NULL condition in your query and try like this

SELECT * FROM `datatablecoulmn` WHERE `FkID` IS NULL

For handling NULL values MySQL provides three operators

IS NULL: operator returns true if column value is NULL.

IS NOT NULL: operator returns true if column value is not NULL.

<=>: operator compares values, which (unlike the = operator) is true even for two NULL values.

You can refer to these links for more

Link 1,Link 2,Link 3

like image 116
Deepu Avatar answered Mar 03 '23 20:03

Deepu