Você está na página 1de 13

MySQL - NULL Values

NULL Value
Conceptually, NULL means a missing unknown
value and it is treated somewhat differently from
other values
Arithmetic comparison operators such as =, <, or <>
cannot be used to test for NULL values
Conditions involving NULL are special and so we
cannot use = NULL or != NULL to look for NULL
values in columns
Such comparisons always fail because it's
impossible to tell whether or not they are true and
even NULL = NULL fails
Handling NULL Values
An SQL SELECT command along with WHERE clause is
used to fetch data from MySQL table, but when we try
to give a condition, which compare field or column
value to NULL, it does not work properly with an
equal to( = ) operator
To handle such situation 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
Using = and != for NULL Values
We will see that =
SELECT * FROM tcount_tbl
and != do not work
WHERE tutorial_count = NULL; with NULL values
SELECT * FROM tcount_tbl because it returns
WHERE tutorial_count != NULL;Empty Set
Tutoria Tutorial_title Tutorial_author Submission_d Tutorial_author Tutorial_cnt
l_id ate

1 Learn PHP Akansha 2016-08-25 ShivaniNanda 12


2 JAVA Tutorials Kanchan 2016-08-27 Srestha Anand 7
Kumari
Sujay Chatterjee NULL
3 PHP for Srestha Anand 2016-08-27
Beginners Annan Naidu 9
4 C# Abhi Mitra 2016-08-27 Kanchan Kumari NULL
5 SAP Sujay 2016-08-31
Chatterjee Akansha 7
MySQL Working with NULL Values

Because the result of any arithmetic comparison


with NULL is also NULL, you cannot obtain any meaningful
results from such comparisons
In MySQL, 0 or NULL means false and anything else means
true with default truth value from a boolean operation is 1
Two NULL values are regarded as equal in a GROUP BY
When doing an ORDER BY, NULL values are presented first if
you do ORDER BY ... ASC and last if you do ORDER BY.. DESC
A common error when working with NULL is to assume that
it is not possible to insert a zero or an empty string into a
column defined as NOT NULL, but this is not the case
These are in fact values, whereas NULL means not having
a value which can be tested easily by using IS [NOT] NULL
NULL Values Interpretation
IS NULL / IS NOT NULL Operators
To find records where the tutorial_count column IS NULL
or IS NOT NULL, the queries should be written like this
SELECT * FROM tcount_tbl
WHERE tutorial_count IS NULL;
SELECT * from tcount_tbl
WHERE tutorial_count IS NOT NULL;
Tutorial_author Tutorial_cnt Tutorial_author Tutorial_cnt

ShivaniNanda 12
Sujay Chatterjee NULL
Srestha Anand 7
Kanchan Kumari NULL Annan Naidu 9
Akansha 7
MySQL ISNULL() function

MySQL ISNULL() function returns 1 when the


expression is NULL otherwise it returns 0
Syntax : ISNULL(expr)
Example : ISNULL() function with non-null value
In the following MySQL statement, given argument
is a non-NULL value. So , ISNULL function returns 0
MySQL ISNULL() function

Example : ISNULL() function with NULL value


In the following MySQL statement, given argument
is a non-NULL value. So , ISNULL function returns 0
SELECT ISNULL(NULL)
Example of ISNULL ( ) Function
<?php
$dbhost="localhost"; $dbuser="root"; $dbpass="";
$conn = mysqli_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{ die('Could not connect: ' . mysqli_error($conn)); }
echo "<tr>";
foreach($conn->query('SELECT ISNULL(1+0)') as $row)
{ echo "<td>" . $row['ISNULL(1+0)'] . "</td>"; }
foreach($conn->query('SELECT ISNULL(0)') as $row)
{ echo "<td>" . $row['ISNULL(0)'] . "</td>"; }
foreach($conn->query('SELECT ISNULL(NULL)') as $row)
{ echo "<td>" . $row['ISNULL(NULL)'] . "</td>"; echo "</tr>";
}
MySQL IFNULL() function

MySQL IFNULL() takes two expressions


and if the first expression is not NULL, it
returns the first expression else it returns
the second expression
Depending on the context in which it is
used, it returns either numeric or string
value
Syntax : IFNULL(expression1,
expression2);
MySQL IFNULL() Function Examples
Example1 : IFNULL() function with NOT NULL first
argument SELECT IFNULL(0,2);
The above MySQL statement returns the first
expression, i.e. 0, since the first expression is not NULL
Example2 : IFNULL() function with NON ZERO first
argument SELECT IFNULL(1,2);
The above MySQL statement returns the first
expression, i.e. 1, since the first expression is not NULL
Example3 : IFNULL() function with NULL first
argument SELECT IFNULL(NULL,2);
The above MySQL statement returns the second
expression, i.e. 2, since the first expression is NULL

Você também pode gostar