3.13.1 Problem
You're trying to negate a condition that involves NULL, but it's not working.
3.13.2 Solution
NULL is special in negations, just like it is otherwise. Perhaps even more so.
3.13.3 Discussion
Recipe 3.10 pointed out that you can reverse query conditions, either by changing comparison operators and Boolean operators, or by using NOT. These techniques may not work if a column can contain NULL. Recall that the taxpayer table from Recipe 3.12 looks like this:
+---------+--------+ | name | id | +---------+--------+ | bernina | 198-48 | | bertha | NULL | | ben | NULL | | bill | 475-83 | +---------+--------+
Now suppose you have a query that finds records with taxpayer ID values that are lexically less than 200-00:
mysql> SELECT * FROM taxpayer WHERE id < '200-00'; +---------+--------+ | name | id | +---------+--------+ | bernina | 198-48 | +---------+--------+
Reversing this condition by using >= rather than < may not give you the results you want. It depends on what information you want to obtain. If you want to select only records with non-NULL ID values, >= is indeed the proper test:
mysql> SELECT * FROM taxpayer WHERE id >= '200-00'; +------+--------+ | name | id | +------+--------+ | bill | 475-83 | +------+--------+
But if you want all the records not selected by the original query, simply reversing the operator will not work. NULL values fail comparisons both with < and with >=, so you must add an additional clause specifically for them:
mysql> SELECT * FROM taxpayer WHERE id >= '200-00' OR id IS NULL; +--------+--------+ | name | id | +--------+--------+ | bertha | NULL | | ben | NULL | | bill | 475-83 | +--------+--------+
Using the mysql Client Program
Writing MySQL-Based Programs
Record Selection Techniques
Working with Strings
Working with Dates and Times
Sorting Query Results
Generating Summaries
Modifying Tables with ALTER TABLE
Obtaining and Using Metadata
Importing and Exporting Data
Generating and Using Sequences
Using Multiple Tables
Statistical Techniques
Handling Duplicates
Performing Transactions
Introduction to MySQL on the Web
Incorporating Query Resultsinto Web Pages
Processing Web Input with MySQL
Using MySQL-Based Web Session Management
Appendix A. Obtaining MySQL Software
Appendix B. JSP and Tomcat Primer
Appendix C. References