英文:
Join a MySQL table to itself + Operation
问题
I receive free updates for some forex currency pairs. I am trying to calculate the other pairs by deduction from those with a numerator and a denominator.
我的MySQL表格(名为param_forex):
我的MySQL查询:
UPDATE `param_forex` as pf
JOIN `param_forex` as pf1 on pf1.numerator = pf.Ticker
JOIN `param_forex` as pf2 on pf2.denominator = pf.Ticker
SET pf.Rate = (SELECT pf1.`Rate` WHERE pf1.`Ticker`= pf1.numerator / SELECT pf2.`Rate` WHERE pf2.`Ticker`= pf2.denominator)
WHERE pf.Source='Calc';
结果:0行受影响。(查询耗时0.0007秒。)
预期输出是对每一行,当来源为'Calc'时,通过获取与Ticker列匹配的适当值来执行分子除以分母,并将结果放入Rate列。
我不明白为什么它不起作用,我已经花了几个小时在上面。(对于之前回答过我的问题的人,希望你们注意到我的查询变得更好了...)
英文:
I receive free updates for some forex currency pairs. I am trying to calculate the other pairs by deduction from those with anumerator and a denominator.
my MyQSL table (named param_forex):
my MySQL query
UPDATE `param_forex` as pf
JOIN `param_forex` as pf1 on pf1.numerator = pf.Ticker
JOIN `param_forex` as pf2 on pf2.denominator = pf.Ticker
SET pf.Rate = (SELECT pf1.`Rate` WHERE pf1.`Ticker`= pf1.numerator / SELECT pf2.`Rate` WHERE pf2.`Ticker`= pf2.denominator)
WHERE pf.Source='Calc'
Result : 0 rows affected. (Query took 0.0007 seconds.)
The expected output is for each line where the source is 'calc' to do the numerator divided by the denominator by fetching the appropriate values matching in the ticker columns and put the result in the rate column
I don't understand why it won't work, and I have spent hours on it.
(for those who have replied to a couple of my questions before, I hope you notice my queries are getting better...)
答案1
得分: 1
不需要子查询,直接在SET
子句中使用pf1.Rate
和pf2.Rate
。
此外,您的ON
条件是颠倒的,因为numerator
和denominator
仅在Source = Calc
的行中设置。
UPDATE `param_forex` as pf
JOIN `param_forex` as pf1 on pf.numerator = pf1.Ticker
JOIN `param_forex` as pf2 on pf.denominator = pf2.Ticker
SET pf.Rate = pf1.Rate / pf2.Rate
WHERE pf.Source='Calc'
英文:
You don't need the subquery, just use pf1.Rate
and pf2.Rate
directly in the SET
clause.
You also have your ON
conditions backwards, since numerator
and denominator
are only set in the rows with Source = Calc
.
UPDATE `param_forex` as pf
JOIN `param_forex` as pf1 on pf.numerator = pf1.Ticker
JOIN `param_forex` as pf2 on pf.denominator = pf2.Ticker
SET pf.Rate = pf1.Rate / pf2.Rate
WHERE pf.Source='Calc'
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论