将一个MySQL表与自身连接 + 操作

huangapple go评论69阅读模式
英文:

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表与自身连接 + 操作

我的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):

将一个MySQL表与自身连接 + 操作

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.Ratepf2.Rate

此外,您的ON条件是颠倒的,因为numeratordenominator仅在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'

huangapple
  • 本文由 发表于 2023年5月25日 00:03:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76325458.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定