Latest web development tutorials

l'elaborazione del valore NULL MySQL

Sappiamo già MySQL utilizzando il comando SQL SELECT con clausola WHERE per leggere i dati nella tabella di dati, ma i campi di criteri di query forniti, quando NULL, il comando potrebbe non funzionare correttamente quando.

Per gestire questo caso, MySQL offre tre principali operatori:

  • IS NULL: Quando il valore della colonna è NULL, l'operatore restituisce true.
  • IS NOT NULL: Quando il valore della colonna non è NULL, l'operatore restituisce true.
  • <=>: Gli operatori di confronto (= diverse da parte dell'operatore) e restituisce vero quando si confrontano i due valore è NULL.

operazione di confronto condizione Chi NULL è speciale. Non è possibile utilizzare = NULL o! = NULL trovare valori NULL nella colonna.

In MySQL, i valori NULL confrontare con qualsiasi altro valore (anche NULL) restituisce sempre false, cioè NULL = NULL restituisce false.

MySQL manipolazione NULL usando la IS NULL e IS NOT NULL operatore.


valori NULL nel prompt dei comandi

Il seguente esempio presuppone che il database w3big tabelle tcount_tbl contenente due w3big_author e w3big_count, w3big_count set inserire i valori NULL.

Esempi

Provare i seguenti esempi:

root@host# mysql -u root -p password;
Enter password:*******
mysql> use w3big;
Database changed
mysql> create table tcount_tbl
    -> (
    -> w3big_author varchar(40) NOT NULL,
    -> w3big_count  INT
    -> );
Query OK, 0 rows affected (0.05 sec)
mysql> INSERT INTO tcount_tbl
    -> (w3big_author, w3big_count) values ('mahran', 20);
mysql> INSERT INTO tcount_tbl
    -> (w3big_author, w3big_count) values ('mahnaz', NULL);
mysql> INSERT INTO tcount_tbl
    -> (w3big_author, w3big_count) values ('Jen', NULL);
mysql> INSERT INTO tcount_tbl
    -> (w3big_author, w3big_count) values ('Gill', 20);

mysql> SELECT * from tcount_tbl;
+-----------------+----------------+
| w3big_author | w3big_count |
+-----------------+----------------+
| mahran          |             20 |
| mahnaz          |           NULL |
| Jen             |           NULL |
| Gill            |             20 |
+-----------------+----------------+
4 rows in set (0.00 sec)

mysql>

L'esempio che segue si può vedere = e = operatore non funziona !:

mysql> SELECT * FROM tcount_tbl WHERE w3big_count = NULL;
Empty set (0.00 sec)
mysql> SELECT * FROM tcount_tbl WHERE w3big_count != NULL;
Empty set (0.01 sec)

Trovare la tabella dei dati w3big_count se la colonna è NULL, è necessario utilizzare la IS NULL e non è nullo, i seguenti esempi:

mysql> SELECT * FROM tcount_tbl 
    -> WHERE w3big_count IS NULL;
+-----------------+----------------+
| w3big_author | w3big_count |
+-----------------+----------------+
| mahnaz          |           NULL |
| Jen             |           NULL |
+-----------------+----------------+
2 rows in set (0.00 sec)
mysql> SELECT * from tcount_tbl 
    -> WHERE w3big_count IS NOT NULL;
+-----------------+----------------+
| w3big_author | w3big_count |
+-----------------+----------------+
| mahran          |             20 |
| Gill            |             20 |
+-----------------+----------------+
2 rows in set (0.00 sec)

Utilizzare PHP valore NULL elaborazione dello script

script PHP è possibile se ... else per gestire se una variabile è vuota, e genera l'istruzione condizionale corrispondente.

I seguenti esempi impostati $ variabile w3big_count PHP, quindi utilizzare la variabile con la tabella di dati w3big_count campi di confronto:

<?php
$dbhost = 'localhost:3036';
$dbuser = 'root';
$dbpass = 'rootpassword';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
  die('Could not connect: ' . mysql_error());
}
if( isset($w3big_count ))
{
   $sql = 'SELECT w3big_author, w3big_count
           FROM  tcount_tbl
           WHERE w3big_count = $w3big_count';
}
else
{
   $sql = 'SELECT w3big_author, w3big_count
           FROM  tcount_tbl
           WHERE w3big_count IS $w3big_count';
}

mysql_select_db('w3big');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
  die('Could not get data: ' . mysql_error());
}
while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
    echo "Author:{$row['w3big_author']}  <br> ".
         "Count: {$row['w3big_count']} <br> ".
         "--------------------------------<br>";
} 
echo "Fetched data successfully\n";
mysql_close($conn);
?>