我有这个MySQL表:
+----+-------------+----------+------+-------+
| ID | DESCRIPTION | QUANTITY | EACH | PRICE |
+----+-------------+----------+------+-------+
| 1 | Product 1 | 1 | 12 | 1*12 |
| 2 | Product 2 | 2 | 3 | 2* 3 |
| 3 | Product 3 | NULL | 3 | |
| 4 | Product 4 | 0 | 7 | |
+----+-------------+----------+------+-------+
这个查询:
SELECT
DESCRIPTION,
QUANTITY,
EACH,
COALESCE(QUANTITY, 1) * EACH AS PRICE
FROM table1
我想用1替换NULL和0,我想在PRICE列中进行乘法运算。我不想使用UPDATE,因为我无法更改table1中的值。 谢谢!
答案 0 :(得分:2)
尝试以下查询
SELECT
DESCRIPTION,
QUANTITY,
EACH,
COALESCE(IF(QUANTITY = NULL OR QUANTITY = 0,1,QUANTITY), 1) * EACH AS PRICE
FROM table1
答案 1 :(得分:1)
SELECT
DESCRIPTION,
QUANTITY,
EACH,
IF(QUANTITY,QUANTITY, 1) * EACH AS PRICE
FROM table1
答案 2 :(得分:0)
听起来你想要CASE
:
SELECT DESCRIPTION, QUANTITY, EACH,
((CASE WHEN QUANTITY IS NULL THEN 0
WHEN QUANTITY = 0 THEN 1
ELSE QUANTITY
END) * EACH
) AS PRICE
FROM table1;