在 case 语句中使用来自 select 语句的参数

Using parameters from select statement inside case statement

我有以下查询

SELECT product.*,
(SELECT bid FROM server1 WHERE product_id = product.id) AS bid1,
(SELECT bid FROM server2 WHERE product_id = product.id) AS bid2,
CASE
    WHEN bid1 > bid2 THEN '1 wins'
    WHEN bid2 > bid1 THEN '2 wins'
    ELSE 'undecided'
END AS bid_status
FROM product
WHERE id= 

此查询在 CASE 语句中抛出错误,指出 bid1 列不存在。如何将子查询作为 case 语句的变量传入?

范围问题。首先将您的查询包装为派生 table:

select dt.*
    CASE
        WHEN bid1 > bid2 THEN '1 wins'
        WHEN bid2 > bid1 THEN '2 wins'
        ELSE 'undecided'
    END AS bid_status
from
(
SELECT product.*,
    (SELECT bid FROM server1 WHERE product_id = product.id) AS bid1,
    (SELECT bid FROM server2 WHERE product_id = product.id) AS bid2,
FROM product
WHERE id= 
) dt