如何在mysql中插入一些公式?

如何在mysql中插入一些公式?,mysql,select,formula,Mysql,Select,Formula,我想计算“柜台”桌上的一行。我试着把我的桌子做成: name black yellow white qty_job total david 1 0 0 2 ? andrew 0 1 1 4 ? 计算公式为: total = (nblack *

我想计算“柜台”桌上的一行。我试着把我的桌子做成:

name          black     yellow     white        qty_job      total
david           1        0          0            2             ?
andrew          0        1          1            4              ?
计算公式为:

total = (nblack * 1) + (nyellow * 1) + (nwhite * 0.4) / qty_job
total = (1 * 1) + (0 * 1) + (0 * 0.4) / 2 = 0.5

如何在mysql代码中插入此公式?尤其是在SELECT方法中。

您不应该/不能使用某个公式排一行。您应该使用此查询检索总计:

SELECT
    name,
    black,
    yellow,
    white,
    qty_job
    (SUM(black) + SUM(yellow) + SUM(white)*0.4) / qty_job AS total
FROM counter
GROUP BY name;

另一种方法是创建视图:

CREATE VIEW test AS
SELECT id, (black * 1) + (yellow * 1) + (white * 0.4) / qty_job as total FROM counter;
剩下的应该很简单,你可以这样做:

select
 counter.id,
 black,
 yellow,
 white,
 test.total
from
 counter,
 test
where
  counter.id = test.id
DECLARE @Number As int, @Number2 As int
SET @Number = 5
WHILE @Number >= 1
BEGIN
PRINT @Number
SET @Number = @Number - 1

PRINT @Number2
SET @Number2 = @Number * (@Number2 - 1)

PRINT 'The Factorial of'
PRINT @Number
PRINT 'is'
PRINT @Number2

END 
GO