gpt4 book ai didi

php - 使用 if 语句更新表

转载 作者:行者123 更新时间:2023-11-29 06:36:43 24 4
gpt4 key购买 nike

我想用if语句更新A表中的价格,我试过了,但是不行,只能一一调用id来计算,如何直接计算价格?这是表格:

表A

-------------------------
id | type | total | price
-------------------------
1 | a1 | 80 | [ ]
2 | a2 | 50 | [ ]
3 | a2 | 15 | [ ]

这是 if 语句

        if($type=='a1')
{
$price1=1050;
$price2=1500;
$price3=2000;
if($total <= 10)
{
$price = $total*$price1;
}
elseif($total <= 20)
{
$a = 10;
$remtotal = $total-$a;
$price = ($a*$price1)+($remtotal*$price2);
}
elseif($total > 20)
{
$a = 10;
$b = 10;
$remtotal = ($total-$a)-$b;
$price = ($a*$price1)+($b*$price2)+($remtotal*$price3);
}

}
elseif($type=='a2')
{
$price1=2100;
$price2=3000;
$price3=4000;
if($total <= 10)
{
$price = $total*$price1;
}
elseif($total <= 20)
{
$a = 10;
$remtotal = $total-$a;
$price = ($a*$price1)+($remtotal*$price2);
}
elseif($total > 20)
{
$a = 10;
$b = 10;
$remtotal = ($total-$a)-$b;
$price = ($a*$price1)+($b*$price2)+($remtotal*$price3);
}
}



SELECT id,type,total,price FROM TABLE A WHERE id='id'


UPDATE TABLE A SET price='$price' WHERE id=id"

我可以通过调用id来一一数但是我希望能立即一次性统计

预期结果

-------------------------
id | type | total | price
-------------------------
1 | a1 | 80 | [145500]
2 | a2 | 50 | [171000]
3 | a2 | 15 | [28500]

怎么做?

<小时/>

已解决

我创建了 2 个表

        TABLE A
-------------------------
id | type | total | price
-------------------------
1 | a1 | 80 | [ ? ]
2 | a2 | 50 | [ ? ]
3 | a2 | 15 | [ ? ]


TABLE B
| type | price1 | price2 | price3 |
| ---- | ------ | ------ | ------ |
| a1 | 1050 | 1500 | 2000 |
| a2 | 2100 | 3000 | 4000 |
| a2 | 2100 | 3000 | 4000 |

这是代码

UPDATE tabelA ta, tabelB tb
SET ta.price = ( CASE
WHEN ta.type = 'a1' AND ta.total <= 10
THEN ta.total * tb.price1

WHEN ta.type = 'a1' AND ta.total <= 20
THEN (10 * tb.price1) + ((ta.total - 10) * tb.price2)

WHEN ta.type = 'a1' AND ta.total > 20
THEN (10 * tb.price1) + (10 * tb.price2) + (((ta.total - 10) - 10) * tb.price3)

WHEN ta.type = 'a2' AND ta.total <= 10
THEN ta.total * tb.price1

WHEN ta.type = 'a2' AND ta.total <= 20
THEN (10 * tb.price1) + ((ta.total - 10) * tb.price2)

WHEN ta.type = 'a2' AND ta.total > 20
THEN (10 * tb.price1) + (10 * tb.price2) + (((ta.total - 10) - 10) * tb.price3)
END )
WHERE ta.type = tb.type


TABLE A
-------------------------
id | type | total | price
-------------------------
1 | a1 | 80 | [145500]
2 | a2 | 50 | [171000]
3 | a2 | 15 | [28500]

最佳答案

最简单的事情是制作价格表。

创建表/插入

CREATE TABLE prices (
`id` INTEGER,
`type` VARCHAR(2),
`price1` DOUBLE,
`price2` DOUBLE,
`price3` DOUBLE
);

INSERT INTo prices (id, type, price1, price2, price3) VALUES(1, 'a1', 1050, 1500, 2000);

以及 INNER JOIN 与您的真实表并在 CASE END 子句中进行计算。

查询

SELECT 
*
, (
CASE
WHEN Table1.type = 'a1' AND Table1.total <= 10
THEN Table1.total * prices.price1

WHEN Table1.type = 'a1' AND Table1.total <= 20
THEN (10 * prices.price1) + ((Table1.total - 10) * prices.price2)

WHEN Table1.type = 'a1' AND Table1.total > 20
THEN (10 * prices.price1) + (10 * prices.price2) + (((Table1.total - 10) - 10) * prices.price3)
END
) AS price_total
FROM
Table1
INNER JOIN
prices
ON
Table1.type = prices.type

结果

| id  | type | total | id  | type | price1 | price2 | price3 | price_total |
| --- | ---- | ----- | --- | ---- | ------ | ------ | ------ | ----------- |
| 1 | a1 | 80 | 1 | a1 | 1050 | 1500 | 2000 | 145500 |

demo

sorry for that , I have add what I expected output

已编辑

我刚刚注意到类型 a1a2 的 PHP 计算代码是相同的,所以你在那里重复自己。

查询应该/可以没有类型。

查询

SELECT 
*
, (
CASE
WHEN Table1.total <= 10
THEN Table1.total * prices.price1

WHEN Table1.total <= 20
THEN (10 * prices.price1) + ((Table1.total - 10) * prices.price2)

WHEN Table1.total > 20
THEN (10 * prices.price1) + (10 * prices.price2) + (((Table1.total - 10) - 10) * prices.price3)
END
) AS price_total
FROM
Table1
INNER JOIN
prices
ON
Table1.type = prices.type

结果

| id  | type | total | id  | type | price1 | price2 | price3 | price_total |
| --- | ---- | ----- | --- | ---- | ------ | ------ | ------ | ----------- |
| 1 | a1 | 80 | 1 | a1 | 1050 | 1500 | 2000 | 145500 |
| 2 | a2 | 50 | 1 | a2 | 2100 | 3000 | 4000 | 171000 |
| 3 | a2 | 15 | 1 | a2 | 2100 | 3000 | 4000 | 36000 |

demo

否则,您需要像这样在查询中重复它。

查询

SELECT 
*
, (
CASE
WHEN Table1.type = 'a1' AND Table1.total <= 10
THEN Table1.total * prices.price1

WHEN Table1.type = 'a1' AND Table1.total <= 20
THEN (10 * prices.price1) + ((Table1.total - 10) * prices.price2)

WHEN Table1.type = 'a1' AND Table1.total > 20
THEN (10 * prices.price1) + (10 * prices.price2) + (((Table1.total - 10) - 10) * prices.price3)


WHEN Table1.type = 'a2' AND Table1.total <= 10
THEN Table1.total * prices.price1

WHEN Table1.type = 'a2' AND Table1.total <= 20
THEN (10 * prices.price1) + ((Table1.total - 10) * prices.price2)

WHEN Table1.type = 'a2' AND Table1.total > 20
THEN (10 * prices.price1) + (10 * prices.price2) + (((Table1.total - 10) - 10) * prices.price3)
END
) AS price_total
FROM
Table1
INNER JOIN
prices
ON
Table1.type = prices.type

关于php - 使用 if 语句更新表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53517637/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com