引用选择语句的结果



我正在研究一个需要比较两个值的存储过程。这是我的 sql 代码

SELECT customer_id, max_rentals
FROM accounting inner join rental_plans
ON accounting.rental_plan_id = rental_plans.rental_plan_id
WHERE customer_id = 0

这将返回:

customer_id max_rentals
----------- -----------
0           3

我想写一个声明,说如果客户 0 的最大租金值是 3,那就做点什么。如何在条件语句中引用该值?

提前谢谢。

这会按您的预期工作吗?

UPDATE <table>
SET <update field>=<value>
WHERE <table>.Customer_id IN (
    SELECT customer_id
    FROM accounting inner join rental_plans
    ON accounting.rental_plan_id = rental_plans.rental_plan_id
    WHERE max_rentals=3
)

我认为你正在努力实现这样的事情。

使用 case 语句,您可以检查 max_rentals = 3 是否然后执行某些操作。

SELECT customer_id, max_rentals,
case   when max_rentals=3 then 'do something' else max_rentals end
FROM   accounting inner join rental_plans
ON     accounting.rental_plan_id = rental_plans.rental_plan_id
WHERE  customer_id = 0;

最新更新