建表及資料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分,都出了
uj5u.com熱心網友回復:
看下10.2的select * from v2結果是什么uj5u.com熱心網友回復:
2條記錄啊,沒有問題
uj5u.com熱心網友回復:
單獨查尋單表,單視圖都沒有問題
uj5u.com熱心網友回復:
問題可能問的不太明確,我再描述一下。資料庫引擎都是innodb
業務是主子表聯合查詢,一對多關系,查詢主表部分欄位和子表最新一條記錄的部分欄位。記錄集行數以主表為準。
mariadb 10.1.21中 運行結果ok,符合預期。
mariadb 10.2.11中 運行結果wrong, 丟失左表資料。
再繼續查原因,發現,如果left join table 時 ok,view 時 wrong
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/93802.html
標籤:MySQL
