如何修复查询条件或其他任何内容
Posted
技术标签:
【中文标题】如何修复查询条件或其他任何内容【英文标题】:How to fix query where condition or anything else 【发布时间】:2019-01-08 05:39:22 【问题描述】:预期的输出是传递的数据 但是在运行查询时,即使断开连接也无法正常工作 它发生在添加'和pass_fail like 'pass''这个条件时
我使用的是 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
【问题讨论】:
【参考方案1】:如果您使用 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 子句 a 重构查询
所以
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
........
看看for where条件
where serial_no >= '%s'
and serial_no <= '%s'
等于
where serial_no = '%s'
...
你不应该使用旧的基于逗号分隔的隐式连接sintax和where条件,而是使用显式连接sintax
FROM A
INNER JOIN (
.....
) C ON A.serial_no = C.serial_no
...
【讨论】:
以上是关于如何修复查询条件或其他任何内容的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 buildArguments 或其他任何东西在 Flutter/FirebaseAnimatedList 中查询? (请举例)