Weird result with LEFT/RIGHT JOIN
am 25.03.2003 23:45:08 von Bernardo Innocenti=2D----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Hello,
the following test case produces an unexpected result
on MySQL (both versions 3.23.54 and 4.0.12), while it
works fine with PostgreSQL and Firebird.
The third query is the wrong one. The first two are
there just to show that partial results are correct.
=2D ------------------------------------------------------------ ----
CREATE TABLE a (id integer, name char(30));
CREATE TABLE b (id integer, name char(30));
CREATE TABLE ab (a_id integer, b_id integer);
INSERT INTO a (id, name) VALUES (1, 'Alpha');
INSERT INTO a (id, name) VALUES (2, 'Bravo');
INSERT INTO a (id, name) VALUES (3, 'Charlie');
INSERT INTO a (id, name) VALUES (4, 'Delta');
INSERT INTO b (id, name) VALUES (101, 'Linus');
INSERT INTO b (id, name) VALUES (102, 'Alan');
INSERT INTO b (id, name) VALUES (103, 'Marcelo');
INSERT INTO b (id, name) VALUES (104, 'Andrew');
INSERT INTO ab (a_id, b_id) VALUES (1, 101);
INSERT INTO ab (a_id, b_id) VALUES (1, 102);
INSERT INTO ab (a_id, b_id) VALUES (1, 104);
INSERT INTO ab (a_id, b_id) VALUES (2, 101);
SELECT * FROM b INNER JOIN ab ON (b.id =3D ab.b_id AND b.name =3D 'Linus');
=2D -- +------+-------+------+------+
=2D -- | id | name | a_id | b_id |
=2D -- +------+-------+------+------+
=2D -- | 101 | Linus | 1 | 101 |
=2D -- | 101 | Linus | 2 | 101 |
=2D -- +------+-------+------+------+
SELECT * FROM a LEFT JOIN ab ON (a.id =3D ab.a_id);
=2D -- +------+---------+------+------+
=2D -- | id | name | a_id | b_id |
=2D -- +------+---------+------+------+
=2D -- | 1 | Alpha | 1 | 101 |
=2D -- | 1 | Alpha | 1 | 102 |
=2D -- | 1 | Alpha | 1 | 104 |
=2D -- | 2 | Bravo | 2 | 101 |
=2D -- | 3 | Charlie | NULL | NULL |
=2D -- | 4 | Delta | NULL | NULL |
=2D -- +------+---------+------+------+
SELECT * FROM b INNER JOIN ab ON (b.id =3D ab.b_id AND b.name =3D 'Linus') =
RIGHT JOIN a ON (a.id =3D ab.a_id);
=2D -- +------+---------+------+------+------+---------+
=2D -- | id | name | a_id | b_id | id | name |
=2D -- +------+---------+------+------+------+---------+
=2D -- | 101 | Linus | 1 | 101 | 1 | Alpha |
=2D -- | 102 | Alan | NULL | NULL | 1 | Alpha |
=2D -- | 103 | Marcelo | NULL | NULL | 1 | Alpha |
=2D -- | 104 | Andrew | NULL | NULL | 1 | Alpha |
=2D -- | 101 | Linus | 2 | 101 | 2 | Bravo |
=2D -- | 102 | Alan | NULL | NULL | 2 | Bravo |
=2D -- | 103 | Marcelo | NULL | NULL | 2 | Bravo |
=2D -- | 104 | Andrew | NULL | NULL | 2 | Bravo |
=2D -- | 101 | Linus | NULL | NULL | 3 | Charlie |
=2D -- | 102 | Alan | NULL | NULL | 3 | Charlie |
=2D -- | 103 | Marcelo | NULL | NULL | 3 | Charlie |
=2D -- | 104 | Andrew | NULL | NULL | 3 | Charlie |
=2D -- | 101 | Linus | NULL | NULL | 4 | Delta |
=2D -- | 102 | Alan | NULL | NULL | 4 | Delta |
=2D -- | 103 | Marcelo | NULL | NULL | 4 | Delta |
=2D -- | 104 | Andrew | NULL | NULL | 4 | Delta |
=2D -- +------+---------+------+------+------+---------+
=2D ------------------------------------------------------------ ----
The result we expect should be:
id | name | a_id | b_id | id | name
=2D -----+----------------+------+------+----+----------
101 | Linus | 1 | 101 | 1 | Alpha
101 | Linus | 2 | 101 | 2 | Bravo
| | | | 3 | Charlie
| | | | 4 | Delta
=2D --=20
// Bernardo Innocenti - Develer S.r.l., R&D dept.
\X/ http://www.develer.com/
=2D----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (GNU/Linux)
iD8DBQE+gNv3ltU4TfxqZsoRAhk8AJ4+OI96Z5BKnh+Oahj/qtOH8iG4mQCe LU6m
J/8/Yco/4hHLeonQTvjo25Y=3D
=3DZO8k
=2D----END PGP SIGNATURE-----
--
MySQL Bugs Mailing List
For list archives: http://lists.mysql.com/bugs
To unsubscribe: http://lists.mysql.com/bugs?unsub=3Dgcdmb-bugs@m.gmane.org