请原谅缺乏格式化! 出于某种原因,我无法弄清楚如何让它出现 非常喜欢每个人......
长时间漫游,第一次海报。我是MySql的新手,我无法理解为什么以下代码不起作用。
UPDATE CUSTOMER_T
SET Cust_Status = 'INACTIVE'
WHERE (SELECT DISTINCT Cust_No, Cust_Last_Name, Cust_Status
FROM CUSTOMER_T NATURAL JOIN BILLING_T
WHERE BILLING_T.Cust_No = BILLING_T.Cust_No
AND (DATEDIFF(BILLING_T.Billing_Due_Date, BILLING_T.Billing_Date_Paid) < (-14)
AND CUSTOMER_T.Cust_Status = "ACTIVE")
OR (CUSTOMER_T.Cust_Status = "ACTIVE"
AND BILLING_T.Billing_Date_Paid IS NULL));
我要做的是找到状态为活动且账单迟到超过14天的任何人
或者它们是活动的并且它们的Billing_Date_Paid字段具有NULL状态。
然后当我找到它们时,我想将其Cust_Status更新为“非活动”。
如果我只是运行以下内容:
SELECT DISTINCT Cust_No, Cust_Last_Name, Cust_Status
FROM CUSTOMER_T NATURAL JOIN BILLING_T
WHERE BILLING_T.Cust_No = BILLING_T.Cust_No
AND (DATEDIFF(BILLING_T.Billing_Due_Date, BILLING_T.Billing_Date_Paid) < (-14)
AND CUSTOMER_T.Cust_Status = "ACTIVE")
OR (CUSTOMER_T.Cust_Status = "ACTIVE"
AND BILLING_T.Billing_Date_Paid IS NULL);
然后我可以找到我正在寻找的人。但是,每当我尝试将此代码合并到UPDATE语句(我发布的第一段代码)时,我都会收到以下错误:
Error Code: 1241. Operand should contain 1 column(s)
答案 0 :(得分:0)
您实际上可以在更新中执行JOIN
,如下所示:
UPDATE CUSTOMER_T a
INNER JOIN BILLING_T b ON
a.Cust_No = b.Cust_No AND
a.Cust_Status = 'ACTIVE' AND
(
b.Billing_Date_Paid IS NULL OR
DATEDIFF(b.Billing_Due_Date, b.Billing_Date_Paid) < -14
)
SET a.Cust_Status = 'INACTIVE'