MySQL Forums
Forum List  »  Newbie

Union and NULL
Posted by: Peter Heiberg
Date: August 10, 2004 03:26AM

I have noticed a strange behaviour when using union. Anyone who knows why this is happening? Is it a bug or a feature?

This is my problem when running the below queries separately without a union I get the following results:

+-----------+-------------+
| folder_id | folder_childid |
+-----------+-------------+
| 1 | 2 |
+-----------+-------------+

+-----------+-------------+
| folder_id | folder_childid |
+-----------+-------------+
| 2 | [NULL] |
| 3 | [NULL] |
+-----------+-------------+
The query:

SELECT folder_parentid AS folder_id, folder_id AS folder_childid
FROM folder
WHERE folder_parentid IS NOT NULL
UNION ALL
SELECT folder_id, null AS folder_childid
FROM folder
WHERE folder_parentid IS NULL

But when I use a UNION ALL on the queries, the NULLs are replaced by 0!

+-----------+-------------+
|__folder_id_|_folder_childid|
+-----------+-------------+
|______1_|________2_|
|______2_|________0_|
|______3_|________0_|
+-----------+-------------+

Why is this?

Options: ReplyQuote


Subject
Written By
Posted
Union and NULL
August 10, 2004 03:26AM
August 10, 2004 06:17AM
August 10, 2004 07:53AM
August 10, 2004 08:29AM
August 11, 2004 04:20AM
August 11, 2004 04:27AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.