How to insert some formula in mysql?

I want to compute one row in the counter table. I am trying to make my table as follows:

name          black     yellow     white        qty_job      total
david           1        0          0            2             ?
andrew          0        1          1            4              ?

formula for calculating:

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

how to insert this formula into mysql code? especially with the SELECT method.

+3
source share
3 answers

You should not / cannot make a line with a specific formula in it. You must use this query to get the total:

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

Another alternative is to create a view:

CREATE VIEW test AS
SELECT id, (black * 1) + (yellow * 1) + (white * 0.4) / qty_job as total FROM counter;

The rest should be easy, you can do something like this:

select
 counter.id,
 black,
 yellow,
 white,
 test.total
from
 counter,
 test
where
  counter.id = test.id
+1
source
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
+1

Source: https://habr.com/ru/post/1761890/


All Articles