where优化中的MySQL查询case语句
我正在用mysql编写一个查询,我对递归查询不太熟悉。 如何以及我需要做什么来优化下面的查询,因为我提出的条件看起来不好,必须有一个更简单的方法来做同样的事情where优化中的MySQL查询case语句,mysql,Mysql,我正在用mysql编写一个查询,我对递归查询不太熟悉。 如何以及我需要做什么来优化下面的查询,因为我提出的条件看起来不好,必须有一个更简单的方法来做同样的事情 select b.entity_id from entity_hierarchies a, entity_hierarchies b where a.entity_id = 25 and a.entity_type = 'user' and b.entity_type = 'idea'
select
b.entity_id
from
entity_hierarchies a,
entity_hierarchies b
where
a.entity_id = 25
and a.entity_type = 'user'
and b.entity_type = 'idea'
and a.Geography_Geography =
case
when
a.Geography_Geography is null
then
a.Geography_Geography
else
b.Geography_Geography
end
and COALESCE(a.Geography_Country, '') =
case
when
a.Geography_Country is null
then
COALESCE(a.Geography_Country, '')
else
b.Geography_Country
end
and COALESCE(a.Geography_DistrictOrCounty, '') =
case
when
a.Geography_DistrictOrCounty is null
then
COALESCE(a.Geography_DistrictOrCounty, '')
else
b.Geography_DistrictOrCounty
end
and COALESCE(a.Geography_State, '') =
case
when
a.Geography_State is null
then
COALESCE(a.Geography_State, '')
else
b.Geography_State
end
and COALESCE(a.Geography_City, '') =
case
when
a.Geography_City is null
then
COALESCE(a.Geography_City, '')
else
b.Geography_City
end
简介
我注意到你可以用更简单的形式重写其中的一些语句
例如:
and a.Geography_Geography =
case
when
a.Geography_Geography is null
then
a.Geography_Geography
else
b.Geography_Geography
end
可以简单地变成:
AND ( a.Geography_Geography is null or a.Geography_Geography = b.Geography_Geography )
最终解决方案
简介
我注意到你可以用更简单的形式重写其中的一些语句
例如:
and a.Geography_Geography =
case
when
a.Geography_Geography is null
then
a.Geography_Geography
else
b.Geography_Geography
end
可以简单地变成:
AND ( a.Geography_Geography is null or a.Geography_Geography = b.Geography_Geography )
最终解决方案