引用select语句的结果

时间:2012-06-18 05:24:31

标签: sql sql-server rdbms

我正在处理需要比较两个值的存储过程。这是我的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,那么就做点什么。如何在条件语句中引用该值?

提前致谢。

2 个答案:

答案 0 :(得分:1)

这会按预期工作吗?

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
)

答案 1 :(得分:0)

我认为你正在努力实现这样的目标。

使用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;