预期的输出是传递数据 但是在运行查询时,即使断开连接也无法正常工作 当在这种情况下添加'和'
我使用MYSQL工作台8.0
select A.serial_no
, A.test01
, C.test02
, D.test03
, E.test04
, F.test05
, G.test06
, H.test07
, I.test08
, J.test09
, K.test10
from
( select serial_no
, test01
from AA
where serial_no >= '%s'
and serial_no <= '%s'
and pass_fail like 'pass'
and register_date IN ( select max(register_date) as date
from AA
where serial_no >= '%s'
and serial_no <= '%s'
group by serial_no )
) A
,(
select serial_no
, test02
from BB
where serial_no >= '%s'
and serial_no <= '%s'
and pass_fail like 'pass'
and register_date IN ( select max(register_date) as date
from BB
where serial_no >= '%s'
and serial_no <= '%s'
group by serial_no)
)B
,(
select serial_no
, test03
from CC
where serial_no >= '%s'
and serial_no <= '%s'
and pass_fail like 'pass'
and register_date IN ( select max(register_date) as date
from CC
where serial_no >= '%s'
and serial_no <= '%s'
group by serial_no)
)D
,(
select serial_no
, test04
from DD
where serial_no >= '%s'
and serial_no <= '%s'
and pass_fail like 'pass'
and register_date IN ( select max(register_date) as date
from DD
where serial_no >= '%s'
and serial_no <= '%s'
group by serial_no)
)E
,(
select serial_no
, test05
from EE
where serial_no >= '%s'
and serial_no <= '%s'
and pass_fail like 'pass'
and register_date IN ( select max(register_date) as date
from EE
where serial_no >= '%s'
and serial_no <= '%s'
group by serial_no)
)F
,(
select serial_no
, test06
from FF
where serial_no >= '%s'
and serial_no <= '%s'
and pass_fail like 'pass'
and register_date IN ( select max(register_date) as date
from FF
where serial_no >= '%s'
and serial_no <= '%s'
group by serial_no)
)G
,(
select serial_no
, test07
from GG
where serial_no >= '%s'
and serial_no <= '%s'
and pass_fail like 'pass'
and register_date IN ( select max(register_date) as date
from GG
where serial_no >= '%s'
and serial_no <= '%s' )
)H
,(
select serial_no
, test08
from HH
where serial_no >= '%s'
and serial_no <= '%s'
and pass_fail like 'pass'
and register_date IN ( select max(register_date) as date
from HH
where serial_no >= '%s'
and serial_no <= '%s' )
)I
,(
select serial_no
, test09
from II
where serial_no >= '%s'
and serial_no <= '%s'
and pass_fail like 'pass'
and register_date IN ( select max(register_date) as date
from II
where serial_no >= '%s'
and serial_no <= '%s' )
)J
,(
select serial_no
, test10
from JJ
where serial_no >= '%s'
and serial_no <= '%s'
and pass_fail like 'pass'
and register_date IN ( select max(register_date) as date
from JJ
where serial_no >= '%s'
and serial_no <= '%s' )
)K
where A.serial_no = C.serial_no
and C.serial_no = D.serial_no
and D.serial_no = E.serial_no
and E.serial_no = F.serial_no
and F.serial_no = G.serial_no
and G.serial_no = H.serial_no
and H.serial_no = I.serial_no
and I.serial_no = J.serial_no
and J.serial_no = K.serial_no
预期结果:传递的数据 实际结果:连接丢失
它不能使用解释查询 https://dev.mysql.com/doc/refman/8.0/en/explain.html
答案 0 :(得分:0)
如果您使用IN子句,并且子查询不对子查询列使用别名
请记住,在IN子句中可用的结果是有限的(这两个原因应该是错误的主要证明)
and register_date IN ( select max(register_date)
from DD
where serial_no >= '%s'
and serial_no <= '%s'
group by serial_no)
为避免IN限制以获得更好的性能,您可以使用内部联接而不是IN子句来重构查询
如此
select A.serial_no
, A.test01
.......
from
( select serial_no
, test01
from AA
INNER JOIN (
select serial_no, max(register_date) as date
from AA
where serial_no = '%s'
group by serial_no
) X ON X.serial_no = AA.serial_no AND X.date = AA.register_date
where pass_fail like 'pass'
) A
........
看看where条件
where serial_no >= '%s'
and serial_no <= '%s'
等于
where serial_no = '%s'
...
您不应使用基于表逗号分隔和条件的旧式隐式联接正弦波,而应使用显式联接正弦波
FROM A
INNER JOIN (
.....
) C ON A.serial_no = C.serial_no
...