我有2张桌子。
表1
CATEGORY | RANGE | PERCENTAGE
REG | 250001 | 50
REG | 500001 | 40
REG | 9999999 | 30
VIP | 5001 | 50
VIP | 10001 | 40
VIP | 9999999 | 30
表2
CATEGORY | PRICE | DISCOUNT
REG | 100000 | 0
REG | 400000 | 0
REG | 600000 | 0
VIP | 3000 | 0
VIP | 6000 | 0
VIP | 120000 | 0
我想根据DISCOUNT
和table 2
RANGE
PERCENTAGE
更新table 1
中的WHEN PRICE is <= 250001
DISCOUNT = PRICE * 50%
WHEN PRICE is > 250001 AND <= 500001
DISCOUNT = PRICE * 40%
WHEN PRICE is > 500001
DISCOUNT = PRICE * 30%
CATEGORY REG
WHEN PRICE is <= 5001
DISCOUNT = PRICE * 50%
WHEN PRICE is > 10001 AND <= 500001
DISCOUNT = PRICE * 40%
WHEN PRICE is > 100001
DISCOUNT = PRICE * 30%
CATEGORY VIP
caused by: org.postgresql.util.PSQLException: ERROR: invalid memory alloc request size 1199606566
Where: PL/pgSQL function if_modified_func() line 51 at assignment
SQL statement "INSERT INTO logged_actions VALUES (audit_row.*)"
PL/pgSQL function if_modified_func() line 58 at SQL statement
SQL statement "INSERT INTO logged_actions VALUES (audit_row.*)"
PL/pgSQL function if_modified_func() line 58 at SQL statement
SQL statement "INSERT INTO logged_actions VALUES (audit_row.*)"
PL/pgSQL function if_modified_func() line 58 at SQL statement
SQL statement "INSERT INTO logged_actions VALUES (audit_row.*)"
PL/pgSQL function if_modified_func() line 58 at SQL statement
SQL statement "INSERT INTO logged_actions VALUES (audit_row.*)"
PL/pgSQL function if_modified_func() line 58 at SQL statement
答案 0 :(得分:1)
试试这个:
UPDATE table2 t2 SET
DISCOUNT = (
SELECT max(t1.`percentage`) * t2.price / 100 FROM table1 t1
WHERE
t1.category = t2.category AND
t2.price < t1.`range`
)
答案 1 :(得分:0)
请尝试以下查询“REG”类别。您可以根据您的要求更改WHEN子句中的类别和条件。
UPDATE table2
SET discount = CASE
WHEN price <= 250001 THEN ((price*50)/100)
WHEN (price BETWEEN 250002 AND 500001) THEN ((price*40)/100)
WHEN price > 500001 THEN ((price*30)/100)
ELSE discount
END
WHERE category = 'REG'
检查类别'REG'的更新查询:
UPDATE table2 SET discount =
(SELECT ((price*a.percentage)/100) FROM table1 as a WHERE price <= a.range AND category='REG' LIMIT 1)
WHERE category = 'REG'
检查以下查询(编辑#2):
UPDATE table2 AS b SET discount =
(SELECT ((b.price*a.percentage)/100) FROM table1 as a WHERE a.range >= b.price AND a.category=b.category LIMIT 1)
确保引用范围列,因为range是MYSQL保留关键字。另请注意,range列的数据类型应为整数。