NULL is not a value - it's a condition. So if you try and write something "= NULL" you'll get an empty set rather than the results you're looking for. Try "IS NULL" instead.
example
Joining 2 tables connecting records that match
mysql> select * from demo_people join demo_property on demo_people.pid = demo_property.pid;
+-----+-----------+--------------+------+------+----------------------+
| pid | name | phone | pid | spid | selling |
+-----+-----------+--------------+------+------+----------------------+
| 1 | Mr Brown | 01225 708225 | 1 | 1 | Old House Farm |
| 3 | Mr Pullen | 01380 724040 | 3 | 2 | The Willows |
| 3 | Mr Pullen | 01380 724040 | 3 | 3 | Tall Trees |
| 3 | Mr Pullen | 01380 724040 | 3 | 4 | The Melksham Florist |
| 3 | Mr Pullen | 01380 724040 | 3 | 6 | The Beetle Drive |
+-----+-----------+--------------+------+------+----------------------+
5 rows in set (0.05 sec)
Joining two tables connecting records that match PLUS an extra for each "orphan" on left
mysql> select * from demo_people left join demo_property on demo_people.pid = demo_property.pid;
+-----+------------+--------------+------+------+----------------------+
| pid | name | phone | pid | spid | selling |
+-----+------------+--------------+------+------+----------------------+
| 1 | Mr Brown | 01225 708225 | 1 | 1 | Old House Farm |
| 2 | Miss Smith | 01225 899360 | NULL | NULL | NULL |
| 3 | Mr Pullen | 01380 724040 | 3 | 2 | The Willows |
| 3 | Mr Pullen | 01380 724040 | 3 | 3 | Tall Trees |
| 3 | Mr Pullen | 01380 724040 | 3 | 4 | The Melksham Florist |
| 3 | Mr Pullen | 01380 724040 | 3 | 6 | The Beetle Drive |
+-----+------------+--------------+------+------+----------------------+
6 rows in set (0.01 sec)
Selecting only orphans from the join - doing it the CORRECT way
mysql> select * from demo_people left join demo_property on demo_people.pid = demo_property.pid where spid is NULL;
+-----+------------+--------------+------+------+---------+
| pid | name | phone | pid | spid | selling |
+-----+------------+--------------+------+------+---------+
| 2 | Miss Smith | 01225 899360 | NULL | NULL | NULL |
+-----+------------+--------------+------+------+---------+
1 row in set (0.00 sec)
Trying to select the orphans, but failing because NULL is not a value
mysql> select * from demo_people left join demo_property on demo_people.pid = demo_property.pid where spid = NULL;
Empty set (0.00 sec)
mysql>
See
More SQL commands for further information sources
(written 2005-04-06 07:55:27)
Associated topics are indexed under
S152 - SQL Primer as Used in MySQL
Some other Articles
Our most popular resourcesCourse PictureMore to programming than just programmingDifferent course every dayNULL in MySQLFree parking for short errands in MelkshamInformation request forms, cleaning up spamSearching security holesA beautiful place to live and learnBusiness practise, 2005 style1634 posts, page by page
Link to page ...
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
13,
14,
15,
16,
17,
18,
19,
20,
21,
22,
23,
24,
25,
26,
27,
28,
29,
30,
31,
32,
33 at 50 posts per page
This is a page archived from The Horse's Mouth at
http://www.wellho.net/horse/ -
the diary and writings of
Graham Ellis.
Every attempt was made to provide current information at the time the
page was written, but things do move forward in our business - new software
releases, price changes, new techniques. Please check back via
our main site for current courses,
prices, versions, etc - any mention of a price in "The Horse's Mouth"
cannot be taken as an offer to supply at that price.
Link to
Ezine home page (for reading).
Link to
Blogging home page (to add comments).