如何使用MySQL表中的其他列在列中进行计算?

如何使用MySQL表中的其他列在列中进行计算?,mysql,sql,Mysql,Sql,我有一张桌子。我想要一个额外的新列名为损益的输出结果 以下是创建该列的条件 我们需要找到第一行的finish_位置是1,对于这个表,它在第6行。因此,这一行及以上行的损益为0.00 我们需要找到差异为10或大于10的第一行,该表位于第18行。然后我们需要将该行的损益分配为10。同样,上述行的损益为0.00 接下来的每一行损益都将增加10,直到本表第23行的finish_位置为1。 最后,我们需要在第23行的损益栏中进行计算 sf_name finish_position o

我有一张桌子。我想要一个额外的新列名为损益的输出结果

以下是创建该列的条件

我们需要找到第一行的finish_位置是1,对于这个表,它在第6行。因此,这一行及以上行的损益为0.00 我们需要找到差异为10或大于10的第一行,该表位于第18行。然后我们需要将该行的损益分配为10。同样,上述行的损益为0.00 接下来的每一行损益都将增加10,直到本表第23行的finish_位置为1。 最后,我们需要在第23行的损益栏中进行计算

  sf_name       finish_position    official_rating       date          bsp       diff
Camanche Grey          11                 NULL         2013-09-18     72.53      0.00
Camanche Grey           4                 NULL         2013-11-08     12.50      0.00
Camanche Grey           3                 NULL         2014-02-27     24.00      0.00
Camanche Grey           6                  65          2014-04-24     32.00      0.00
Camanche Grey          13                  63          2014-05-05    109.06      0.00
Camanche Grey           1                  59          2014-07-04      4.16      0.00
Camanche Grey           2                  65          2014-08-04      5.10     -6.00
Camanche Grey          12                  68          2014-10-28     55.00     -9.00
Camanche Grey           9                  62          2014-12-09    110.89     -3.00
Camanche Grey           5                  65          2015-01-08     61.07     -6.00
Camanche Grey           7                  60          2015-01-29     12.54     -1.00
Camanche Grey          11                  65          2015-04-08     15.50     -6.00
Camanche Grey           5                  63          2015-04-22     36.07     -4.00
Camanche Grey           1                  60          2015-07-14      4.25      0.00
Camanche Grey           9                  60          2015-08-04      5.30      0.00
Camanche Grey           8                  56          2016-01-22     22.67      4.00
Camanche Grey           4                  52          2016-02-26     11.76      8.00
Camanche Grey           7                  50          2016-03-31      4.30     10.00
Camanche Grey           8                  48          2016-04-25      6.31     12.00
Camanche Grey           4                  56          2016-07-12      9.50      4.00
Camanche Grey           8                  47          2016-09-23     23.47     13.00
Camanche Grey           9                  52          2017-06-20    114.54      8.00
Camanche Grey           1                  50          2017-07-04     15.51      0.00
Camanche Grey           3                  55          2017-07-20      5.29     -5.00
Camanche Grey           3                  48          2017-07-29     15.51      2.00
Camanche Grey           2                  47          2017-08-24      8.00      3.00
Camanche Grey          10                  38          2018-01-12     17.70     12.00
Camanche Grey           2                  55          2018-07-19     27.28     -5.00
Camanche Grey           3                  54          2018-07-30      6.78     -4.00
Camanche Grey           1                  54          2018-08-21     24.00      0.00
所以在第23行的损益栏中有95.1的值。你们可以从下面的表格中得到一个清晰的想法

Calculation Formula = (bsp for that row * 10) - (profit/loss for that row)
            example = (15.51 * 10) - (60)  
                    = 155.1 - 60
                    = 95.1
第27行到第30行进行了另一次计算。因为第27行的差异大于12

这里是小提琴链接:

我试过密码。请检查并让我知道…

测试此项:

选择*, @f1:=当@f1=0且完成位置=1时的情况 那么1 ELSE@f1 完f1,, @f2:=当@f1=1且差异>=10时的情况 那么1 ELSE@f2 f2端, @f3:=当@f1=1和@f2=1时的情况 然后@f3+10 其他0 完f3, 当@f3>0且完成位置=1时的情况 然后bsp*10-@f3 ELSE@f3 结束`损益', 当@f1=1和@f2=1且完成位置=1时的情况 然后@f1:=1+@f2:=0+@f3:=0 其他0 结束f4 从测试中,选择@f1:=0、@f2:=0、@f3:=0变量 按日期订购;
我不想在输出中显示与f相关的列–Zajjith Vedha

将此查询用作将排序移动到外部查询的子查询

选择sf_名称、完成_位置、官方_评级、日期C、bsp、差异、`损益` 从…起 选择*, @f1:=当@f1=0且完成位置=1时的情况 那么1 ELSE@f1 完f1,, @f2:=当@f1=1且差异>=10时的情况 那么1 ELSE@f2 f2端, @f3:=当@f1=1和@f2=1时的情况 然后@f3+10 其他0 完f3, 当@f3>0且完成位置=1时的情况 然后bsp*10-@f3 ELSE@f3 结束`损益', 当@f1=1和@f2=1且完成位置=1时的情况 然后@f1:=1+@f2:=0+@f3:=0 其他0 结束f4 从测试中,选择@f1:=0、@f2:=0、@f3:=0变量 子查询 按日期订购;
我根本不懂计算。第18行之前有大于10的值。我们需要在表中找到等于10或大于10的第一个差异值。因此,除了finish_position、date和diff之外,所有列都与此问题无关?我们希望bsp列用于计算Yes。MySQL版本8.0.13我怎么能只显示sf_名称、完成_位置、官方_评级、日期、bsp、损益列的输出?我不想在output@ZajjithVedha将此查询用作将排序移动到外部查询的子查询。。。更新。
   sf_name       finish_position    official_rating       date         bsp       diff     profit/loss
Camanche Grey          11                 NULL         2013-09-18     72.53      0.00         0.00
Camanche Grey           4                 NULL         2013-11-08     12.50      0.00         0.00
Camanche Grey           3                 NULL         2014-02-27     24.00      0.00         0.00
Camanche Grey           6                  65          2014-04-24     32.00      0.00         0.00
Camanche Grey          13                  63          2014-05-05    109.06      0.00         0.00
Camanche Grey           1                  59          2014-07-04      4.16      0.00         0.00
Camanche Grey           2                  65          2014-08-04      5.10     -6.00         0.00
Camanche Grey          12                  68          2014-10-28     55.00     -9.00         0.00
Camanche Grey           9                  62          2014-12-09    110.89     -3.00         0.00
Camanche Grey           5                  65          2015-01-08     61.07     -6.00         0.00
Camanche Grey           7                  60          2015-01-29     12.54     -1.00         0.00
Camanche Grey          11                  65          2015-04-08     15.50     -6.00         0.00
Camanche Grey           5                  63          2015-04-22     36.07     -4.00         0.00
Camanche Grey           1                  60          2015-07-14      4.25      0.00         0.00
Camanche Grey           9                  60          2015-08-04      5.30      0.00         0.00
Camanche Grey           8                  56          2016-01-22     22.67      4.00         0.00
Camanche Grey           4                  52          2016-02-26     11.76      8.00         0.00
Camanche Grey           7                  50          2016-03-31      4.30     10.00        10.00
Camanche Grey           8                  48          2016-04-25      6.31     12.00        20.00
Camanche Grey           4                  56          2016-07-12      9.50      4.00        30.00
Camanche Grey           8                  47          2016-09-23     23.47     13.00        40.00
Camanche Grey           9                  52          2017-06-20    114.54      8.00        50.00
Camanche Grey           1                  50          2017-07-04     15.51      0.00        95.51
Camanche Grey           3                  55          2017-07-20      5.29     -5.00         0.00
Camanche Grey           3                  48          2017-07-29     15.51      2.00         0.00
Camanche Grey           2                  47          2017-08-24      8.00      3.00         0.00
Camanche Grey          10                  38          2018-01-12     17.70     12.00        10.00
Camanche Grey           2                  55          2018-07-19     27.28     -5.00        20.00
Camanche Grey           3                  54          2018-07-30      6.78     -4.00        30.00
Camanche Grey           1                  54          2018-08-21     24.00      0.00       200.00