views:

187

answers:

4

VERSION I'm using Server version: 5.1.36-community-log MySQL Community Server (GPL)

I've finally contrived a easy example to reproduce it easily!

setup:

create table t1(id integer unsigned,link integer unsigned);
create table t2(id integer unsigned auto_increment,primary key(id));
create table t3(id integer unsigned,content varchar(30));
insert into t1 value(1,null);
insert into t2 value(1);
insert into t3 value(1,'test');

then run:

select t2.*,t3.* 
from t1
left join t2 on t1.link=t2.id
left join t3 on t3.id=t2.id
where t1.id=1;

will get this wrongly:

+------+------+---------+
| id   | id   | content |
+------+------+---------+
| NULL |    1 | test    |
+------+------+---------+

But if we create t2 this way,it won't happen:

create table t2(id integer unsigned);

So,it has something to do with primary key!

NEW FOUND

run this will not trigger the bug:

select t2.*,t3.*
from t1
left join t2 on t1.link=t2.id
left join t3 on t2.id=t3.id
where t1.id=1;

So it also has something to do with join direction!

A: 

Is it that one of the 'fid' rows in 'fuinfo' is set to NULL? MySQL could be joining that up with a NULL value in the left table.

Jez
But it's not set to NULL,it's here set to "Tiger"!
Misier
fuinfo.name is set to "Tiger", but it could be being paired up with a row in answers where lefId is NULL.
Jez
But I've specified the join condition to be: ON f.id = a.lefId
Misier
Yeah, but are any of the f.id fields set to NULL? I'm thinking MySQL could be matching NULL with NULL.
Jez
No,there is only one row,of which the id is 1.
Misier
Also: NULL != NULL and should not be joined
Manu
It seems is caused by where clause?Really odd!
Misier
A: 

Very interesting. This indeed does look like a bug in MySQL. The result of the query depends on whether there are primary keys or not. They should definitely not affect the result. For the reference, PostgreSQL will return the correct result with primary keys, so I think it's unlikely that it would be an expected behavior.

Lukáš Lalinský
Can I be paid for reporting or discovering a bug?
Misier
A: 

Your setup code is wrong

create table t1(id integer unsigned,link integer unsigned);  
create table t2(id integer unsigned auto_increment,primary key(id));  
create table t2(id integer unsigned,content varchar(30));

             ^^  This is wrong. Should be t3

Plus be sure you drop your tables after each test. You probably have some wrong data in them.

Alex Bagnolini
It's a typo,please try it locally,you'll definitely meet with the same issue!
Misier
It runs ok in MSSQL, maybe you should specify your DBMS in the title or in the body, instead of only the Tags.
Alex Bagnolini
I don't like being duplicate!
Misier
Alex, it's quite common to only specify it in the tags. It's a good idea to put it in the body, but definitely not necessary.
Darryl Hein
+1  A: 

I just ran you create, insert and select on MySQL 5.0.58, 5.0.82 & 5.1.35 and I received the following result, which I think is correct:

+------+------+---------+
| id   | id   | content |
+------+------+---------+
| NULL | NULL | NULL    |
+------+------+---------+

Here is exactly what I used:

CREATE TABLE `t1` (
  `id` int(10) unsigned default NULL,
  `link` int(10) unsigned default NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

INSERT INTO `t1` VALUES (1, NULL); 

CREATE TABLE `t2` (
  `id` int(10) unsigned NOT NULL auto_increment,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 ;

INSERT INTO `t2` VALUES (1);

CREATE TABLE `t3` (
  `id` int(10) unsigned default NULL,
  `content` varchar(30) default NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

INSERT INTO `t3` VALUES (1, 'test');

SELECT t2.id, t3.id, t3.content
FROM t1
LEFT JOIN t2 ON t1.link = t2.id
LEFT JOIN t3 ON t2.id = t3.id
WHERE t1.id = 1;
Darryl Hein
Oh,then it must be a bug introduce with Server version: 5.1.36-community-log MySQL Community Server (GPL)
Misier
Darryl Hein
Great!I've further found that it's caused by join direction!Please run this:select t2.*,t3.*from t1left join t2 on t1.link=t2.idleft join t3 on t3.id=t2.idwhere t1.id=1;
Misier
Neither produces the bug for me.
Darryl Hein
Are you able to install or test on another version of MySQL?
Darryl Hein
I just tested with MySQL 5.0.37,and no such bug.Switching back to 5.1.36,it occurs again!Maybe you can have a try yourself with 5.1.36.
Misier