別のテーブルからサブクエリを取得する必要がある面白い MySQL クエリがあります。mysql にサブクエリを評価させることが可能かどうか疑問に思っています。
例:(いくつかの括弧を「gte」と「lte」に置き換える必要がありました。これは、投稿形式を台無しにしていたためです)
select a.id,a.alloyname,a.label,a.symbol, g.grade,
if(a.id = 1,(
(((select avg(cost/2204.6) as averageCost from nas_cost where cost != '0' and `date` lte '2011-03-01' and `date` gte '2011-03-31') - t.value) * (astm.astm/100) * 1.2)
),(a.formulae)) as thisValue
from nas_alloys a
left join nas_triggers t on t.alloyid = a.id
left join nas_astm astm on astm.alloyid = a.id
left join nas_estimatedprice ep on ep.alloyid = a.id
left join nas_grades g on g.id = astm.gradeid
where a.id = '1' or a.id = '2'
order by g.grade;
したがって、IF ステートメントが = '1' でない場合、(a.formulae) は nas_alloys テーブルの値であり、次のとおりです。
((ep.estPrice - t.value) * (astm.astm/100) * 0.012)
基本的に、このクエリを次のように実行したい:
select a.id,a.alloyname,a.label,a.symbol, g.grade,
if(a.id = 1,(
(((select avg(cost/2204.6) as averageCost from nas_cost where cost != '0' and `date` gte '2011-03-01' and `date` lte '2011-03-31') - t.value) * (astm.astm/100) * 1.2)
),((ep.estPrice - t.value) * (astm.astm/100) * 0.012)) as thisValue
from nas_alloys a
left join nas_triggers t on t.alloyid = a.id
left join nas_astm astm on astm.alloyid = a.id
left join nas_estimatedprice ep on ep.alloyid = a.id
left join nas_grades g on g.id = astm.gradeid
where a.id = '1' or a.id = '2'
order by g.grade;
a.id != '1' の場合、ところで、a.formulae には約 30 の異なる可能性があり、それらは頻繁に変更されるため、複数の if ステートメントでハードバンギングすることは実際にはオプションではありません。【ビジネスロジックの再設計はそれより可能性が高い!】
とにかく、何か考えはありますか?これでも機能しますか?
-ありがとう -ショーン