Bug #3604 UNION incorporating a LEFT JOIN yields incorrect result set.
Submitted: 29 Apr 2004 20:30 Modified: 29 Apr 2004 21:01
Reporter: John Grogan Email Updates:
Status: Closed Impact on me:
None 
Category:MySQL Server: Command-line Clients Severity:S3 (Non-critical)
Version:4.1.1 OS:Linux (Redhat Linux 9.0)
Assigned to: CPU Architecture:Any

[29 Apr 2004 20:30] John Grogan
Description:
UNION incorporating a LEFT JOIN yields incorrect result set. Below I have attached the result we achieve in 4.1.1 versus the expected result as appears in 4.0

}}} -------------------------------------------------------------------------
}}} Behavior in Mysql 4.1.1
}}} -------------------------------------------------------------------------

mysql> select   concat(Sites_Code,'.',Site_Code) as Value,
    -> concat(Sites_Name,' - ',Site_Name) as Name
    -> from     Sites LEFT JOIN Site on (Site_Sites_ID = Sites_ID)
    -> UNION
    -> select   Sites_Code as Value, Sites_Name as Name
    -> from     Sites;
+--------+---------------+
| Value  | Name          |
+--------+---------------+
| NULL   | NULL          |
| lerner | Lerner Hall   |
| INTL   | International |
+--------+---------------+
3 rows in set (0.00 sec)

}}} -------------------------------------------------------------------------
}}} Behavior in Mysql 4.0.13
}}} -------------------------------------------------------------------------

mysql> (select concat(Sites_Code,'.',Site_Code) as Value,
        concat(Sites_Name,' - ',Site_Name) as Name 
        from Sites LEFT OUTER JOIN Site on (Site_Sites_ID = Sites_ID)) 
        UNION 
        (select Sites_Code as Value, Sites_Name as Name from Sites) 
	order by value;
	
+-------------------+-------------------------------+
| Value             | Name                          |
+-------------------+-------------------------------+
| NULL              | NULL                          |
| INTL              | International                 |
| lerner            | lerner hall                   |
| lerner.lerner_403 | lerner hall - 403 Lerner Hall |
+-------------------+-------------------------------+
4 rows in set (0.01 sec)

}}} --------------------------------------------------------------------------
}}} However, in mySql 4.1, individual queries yield proper results
}}} --------------------------------------------------------------------------

mysql> select   concat(Sites_Code,'.',Site_Code) as Value,
    -> concat(Sites_Name,' - ',Site_Name) as Name
    -> from     Sites LEFT JOIN Site on (Site_Sites_ID = Sites_ID);
+-------------------+-------------------------------+
| Value             | Name                          |
+-------------------+-------------------------------+
| lerner.lerner_403 | Lerner Hall - 403 Lerner Hall |
| NULL              | NULL                          |
+-------------------+-------------------------------+
2 rows in set (0.00 sec)

mysql> select   Sites_Code as Value, Sites_Name as Name
    -> from     Sites;
+--------+---------------+
| Value  | Name          |
+--------+---------------+
| lerner | Lerner Hall   |
| INTL   | International |
+--------+---------------+
2 rows in set (0.00 sec)

How to repeat:
}}} --------------------------------------------------------------------------
}}} To Re-Create
}}} --------------------------------------------------------------------------

CREATE TABLE `Sites` (
     `Sites_ID` int(11) NOT NULL auto_increment,
     `Sites_Code` varchar(50) NOT NULL default '',
     `Sites_Name` varchar(255) default NULL,
     PRIMARY KEY  (`Sites_ID`),
     UNIQUE KEY `Sites_Code` (`Sites_Code`)
     ) TYPE=InnoDB;        

CREATE TABLE `Site` (
     `Site_ID` int(11) NOT NULL auto_increment,
     `Site_Sites_ID` int(11) NOT NULL default '0',
     `Site_Code` varchar(50) NOT NULL default '',
     `Site_Name` varchar(255) default NULL,
     PRIMARY KEY  (`Site_Sites_ID`,`Site_ID`),
     UNIQUE KEY `Site_ID` (`Site_ID`),
     UNIQUE KEY `Site_Code` (`Site_Sites_ID`,`Site_Code`),
     CONSTRAINT `0_928` FOREIGN KEY (`Site_Sites_ID`) REFERENCES `Sites` (`Sites_ID`) ON DELETE CASCADE
     ) TYPE=InnoDB;

insert into Sites values ('', 'lerner', 'lerner hall');

insert into Sites values ('', 'INTL', 'International');

insert into Site values('2', '1', 'lerner_403', '403 Lerner Hall');

select   concat(Sites_Code,'.',Site_Code) as Value,concat(Sites_Name,' - ',Site_Name) as Name
from     Sites LEFT JOIN Site on (Site_Sites_ID = Sites_ID);

select   Sites_Code as Value, Sites_Name as Name
from     Sites;

select   concat(Sites_Code,'.',Site_Code) as Value,concat(Sites_Name,' - ',Site_Name) as Name
from     Sites LEFT JOIN Site on (Site_Sites_ID = Sites_ID)
UNION
select   Sites_Code as Value, Sites_Name as Name
from     Sites;
[29 Apr 2004 21:01] Miguel Solorzano
Thank you for your bug report. This issue has been committed to our
source repository of that product and will be incorporated into the
next release.

If necessary, you can access the source repository and build the latest
available version, including the bugfix, yourself. More information 
about accessing the source trees is available at
    http://www.mysql.com/doc/en/Installing_source_tree.html

Additional info:

mysql> select version();
+-----------------+
| version()       |
+-----------------+
| 4.1.2-alpha-max |
+-----------------+
1 row in set (0.02 sec)

mysql> (select concat(Sites_Code,'.',Site_Code) as Value,
    -> concat(Sites_Name,' - ',Site_Name) as Name
    -> from Sites LEFT OUTER JOIN Site on (Site_Sites_ID = Sites_ID))
    -> UNION
    -> (select Sites_Code as Value, Sites_Name as Name from Sites)
    -> order by value;
+-------------------+-------------------------------+
| Value             | Name                          |
+-------------------+-------------------------------+
| NULL              | NULL                          |
| INTL              | International                 |
| lerner            | lerner hall                   |
| lerner.lerner_403 | lerner hall - 403 Lerner Hall |
+-------------------+-------------------------------+
4 rows in set (0.02 sec)