我想使用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]
答案 0 :(得分:2)
最简单的方法是制作价格表。
创建表/插入
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 |
抱歉,我已经添加了预期的输出
已编辑
我刚刚注意到类型a1
和a2
的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 |
否则,您需要像这样在查询中重复它。
查询
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