建表及数据sql, 如下:
create table `t1`(
id int not null auto_increment,
name varchar(50),
primary key (id)
) engine = innodb auto_increment = 1 default character set = utf8;
create table `t2`(
id int not null auto_increment,
pid int not null,
subname varchar(50),
primary key (id)
) engine = innodb auto_increment = 1 default character set = utf8;
insert into `t1`(id, name) values (1,'a'),(2,'b'),(3,'c');
insert into `t2`(pid, subname) values (1,'a_1'),(1,'a_2'),(1,'a_3'),(2,'b_1'),(2,'b_2'),(2,'b_3');
create table `t2_1` as select * from t2 where id in ( select max(id) from t2 group by pid );
create view `v2` as select * from t2 where id in ( select max(id) from t2 group by pid );查询语句:
select t1.id, t1.name, t22.id as sid, t22.subname from t1 
left join (select * from t2 where id in ( select max(id) from t2 group by pid )) as t22 on (t1.id = t22.pid);select t1.id, t1.name, t2_1.id as sid, t2_1.subname from t1 
left join t2_1 on (t1.id = t2_1.pid);select t1.id, t1.name, t22.id as sid, t22.subname from t1 
left join v2 as t22 on (t1.id = t22.pid);在mariadb 10.1.21 得到期望结果
在mariadb 10.2.11 和 10.2.13 中结果与期望不一致
目前不清楚是什么情况。
请各位帮忙看一下。
就200分,都出了

解决方案 »

  1.   

    看下10.2的select * from v2结果是什么
      

  2.   

    问题可能问的不太明确,我再描述一下。
    数据库引擎都是innodb
    业务是主子表联合查询,一对多关系,查询主表部分字段和子表最新一条记录的部分字段。记录集行数以主表为准。mariadb 10.1.21中 运行结果ok,符合预期。
    mariadb 10.2.11中 运行结果wrong, 丢失左表数据。再继续查原因,发现,如果left join table 时 ok,view 时 wrong