我有一个包含3个价格字段的数据库。 现在我想按以下顺序查询最小价格
如果MIN价格1大于0则价格1
ELSE如果MIN价格2大于0则价格2
ELSE如果MIN价格3大于0则价格3
但我总是得到最低的价格,无论在哪个领域
SELECT f.user,
(SELECT
(
CASE
WHEN MIN(s.price2) > '0' THEN CONCAT(MIN(s.price2), '*Week*')
WHEN MIN(s.price1) > '0' THEN CONCAT(MIN(s.price1), '*Week*')
WHEN MIN(s.price3) > '0' THEN CONCAT(MIN(s.price3), '*Week*')
ELSE 0
END
)
FROM prices s WHERE f.user = s.user
) as price FROM table f WHERE f.user = 84
+------+--------+--------+--------+--------+
| id | user | price1 | price2 | price3 |
+------+--------+--------+--------+--------+
| 1 | 84 | 785 | 0 | 0 |
| 2 | 84 | 550 | 200 | 100 |
| 3 | 84 | 0 | 90 | 45 |
| 4 | 84 | 0 | 0 | 80 |
| 5 | 84 | 0 | 300 | 80 |
| 6 | 84 | 100 | 95 | 0 |
+------+--------+--------+--------+--------+
我想要的是:优先级为MIN price2,然后是MIN price1,然后是MIN price4。
+------+-------+-------+
| id | user | price |
+------+-------+-------+
| 1 | 84 | 90 |
+------+-------+-------+
答案 0 :(得分:0)
我仍然不确定我是否理解你的问题。你拿最低价格:
MIN(price1)
= 100 MIN(price2)
= 90 MIN(price3)
= 45 SUM(price2)
具有最高优先级。因为它大于零,我们返回这个。 (否则我们会检查SUM(price1)
,如果它们为零或更少,我们会检查SUM(price3)
。)
select
case
when sum(price2) > 0 then sum(price2)
when sum(price1) > 0 then sum(price1) end
when sum(price3) > 0 then sum(price3) end
else 0
end as price
from prices
where user = 84;