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

发布于 2024-09-16 08:58:30 字数 452 浏览 2 评论 0原文

我想计算“柜台”表中的一行。我尝试让我的表像:

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 方法中。

i want to calculating one row at table "counter". i try to make my table like:

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

the formula to calculate is :

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 at mysql code? especially at SELECT method.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

吃颗糖壮壮胆 2024-09-23 08:58:30

您不应该/不能创建包含特定公式的行。您应该使用此查询来检索总数:

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

You shouldn't / can't make a row with a certain formula in it. You should use this query to retrieve 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;
仄言 2024-09-23 08:58:30

另一种选择是创建一个视图:

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

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 could do something like this :

select
 counter.id,
 black,
 yellow,
 white,
 test.total
from
 counter,
 test
where
  counter.id = test.id
何时共饮酒 2024-09-23 08:58:30
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
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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文