Wu Yuxiong - natural born MySQL study notes: MySQL NULL value processing

 MySQL uses SQL SELECT command and the WHERE clause to read the data in the table, but the query field provided is NULL, the command may not be able to work as normal.
To handle this situation, MySQL provides three operators:
IS NULL: When the value of the column is NULL, the operator returns true.
IS NOT NULL: column when the value is not NULL, the operator returns true.
<=>: comparison operators (= different from the operator), returns true when comparing the two values are equal or are NULL.
Conditions comparison operations on NULL is rather special. You can not use = NULL or! = NULL find NULL values in the column.
In MySQL compared with any other value NULL value (even NULL) always returns false, i.e., NULL = NULL returns false.
MySQL processing using IS NULL NULL and IS NOT NULL operator.
select * , columnName1+ifnull(columnName2,0) from tableName;
columnName1, columnName2 an int, when columnName2, there is null, ColumnName1 + columnName2 = null, IFNULL (columnName2,0) columnName2 the null values into 0.
NULL values ​​in a command prompt
The following example assumes that the database tables runoob_test_tbl RUNOOB containing two runoob_author and runoob_count, runoob_count insert a NULL value is set.
root@host# mysql -u root -p password;
Enter password:*******
mysql> use RUNOOB;
Database changed
mysql> create table runoob_test_tbl
    -> (
    -> runoob_author varchar(40) NOT NULL,
    -> runoob_count  INT
    -> );
Query OK, 0 rows affected (0.05 sec)
mysql> INSERT INTO runoob_test_tbl (runoob_author, runoob_count) values ('RUNOOB', 20);
mysql> INSERT INTO runoob_test_tbl (runoob_author, runoob_count) values ('Google', NULL);
mysql> INSERT INTO runoob_test_tbl (runoob_author, runoob_count) values ('FK', 20);
You can see the following Examples = and =! Operator does not work:
mysql> SELECT * FROM runoob_test_tbl WHERE runoob_count = NULL;
Empty set (0.00 sec)
mysql> SELECT * FROM runoob_test_tbl WHERE runoob_count != NULL;
Empty set (0.01 sec)
Runoob_test_tbl lookup data table whether the column is NULL, and must use the IS NULL IS NOT NULL, the following examples:
mysql> SELECT * FROM runoob_test_tbl WHERE runoob_count IS NULL;

mysql> SELECT * from runoob_test_tbl WHERE runoob_count IS NOT NULL;
Use PHP script to handle NULL values
PHP script you can IF ... the else statement to handle if the variable is empty, and generates the corresponding conditional statement.
The following examples are provided $ runoob_count PHP variable, then the variable runoob_count field data table is compared:
<?php
dbHost $ = ' localhost: 3306 ' ; // MySQL server host address
dbuser $ = ' root ' ; // MySQL user name
dbpass $ = ' 123456 ' ; // MySQL user name and password
$conn = mysqli_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
    Die ( ' connection failed: ' mysqli_error ($ Conn).);
}
// set the encoding to prevent Chinese garbled
mysqli_query($conn , "set names utf8");
 
if( isset($runoob_count ))
{
   $sql = "SELECT runoob_author, runoob_count
           FROM  runoob_test_tbl
           WHERE runoob_count = $runoob_count";
}
else
{
   $sql = "SELECT runoob_author, runoob_count
           FROM  runoob_test_tbl
           WHERE runoob_count IS NULL";
}
mysqli_select_db( $conn, 'RUNOOB' );
$retval = mysqli_query( $conn, $sql );
if(! $retval )
{
    Die ( ' not read: ' mysqli_error ($ Conn).);
}
while($row = mysqli_fetch_array($retval, MYSQL_ASSOC))
{
    echo "<tr>".
         "<td>{$row['runoob_author']} </td> ".
         "<td>{$row['runoob_count']} </td> ".
         "</tr>";
}
echo '</table>';
mysqli_close($conn);
?>

 

Guess you like

Origin www.cnblogs.com/tszr/p/12113849.html