MySQL评估结果

发布于 2024-10-22 14:54:54 字数 1583 浏览 1 评论 0原文

我有一个有趣的 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 语句中硬敲击并不是真的一个选项。 [重新设计业务逻辑更有可能!]

无论如何,有什么想法吗?这还能用吗?

-谢谢 -肖恩

I have a funny MySQL query that needs to pull a subquery from another table, I'm wondering if this is even possible to get mysql to evaluate the subquery.

example:
(I had to replace some brackets with 'gte' & 'lte' cause they were screwing up the post format)


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;

So when the IF statement is not = '1' then the (a.formulae) is the value in the nas_alloys table which is:

((ep.estPrice - t.value) * (astm.astm/100) * 0.012)

Basically I want this query to run as:


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;

When a.id != '1', btw, there are about 30 different possibilities for a.formulae, and they change frequently, so hard banging in multiple if statements is not really an option. [redesigning the business logic is more likely than that!]

Anyway, any thoughts? Will this even work?

-thanks
-sean

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

衣神在巴黎 2024-10-29 14:54:54

创建一个存储函数来为您计算该值,并传递您稍后决定的参数。当您的业务逻辑发生变化时,您只需更新存储函数即可。

Create a Stored Function to compute that value for you, and pass the params you will decide later on. When your business logic changes, you just have to update the Stored Function.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文