2012-06-18 59 views
0

我就需要比较两个值的存储过程工作的结果。这是我的SQL代码引用select语句

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,那么做一些事情的声明。我如何在条件声明中引用该值?

在此先感谢。

+0

什么意思是“做某事”? – Nalaka526

+0

这是我想做的事情...... 检查是否最大租金@客户ID = 1的值是3,如果再加入25到他们的账户余额 –

回答

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; 
+0

但我要如何检查是否最大的出租CUSTOMER_ID = 3? –

+0

@LuisBarahona where where子句像这样'WHERE customer_id = 3;' –

+0

这看起来是否正确? IF accounting.movi​​es_out> 0其中CUSTOMER_ID = @customer_id THEN –

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 
)