[英]Update different columns in different tables based on column value
I'm trying to what is wrote in the question, probably is a stupid question but I can't find a solution, basically this is what I would like to do: 我正在尝试写问题中的内容,可能是一个愚蠢的问题,但我找不到解决方案,基本上这是我想做的事情:
if(a.operator_id!=0){
UPDATE ".$SupportTicketsTable." a ,".$SupportUserTable." b
SET a.title=? , a.priority=?, a.ticket_status=?,
b.solved_tickets= CASE
WHEN a.ticket_status!='0' THEN (b.solved_tickets+1)
ELSE b.solved_tickets END ,
b.assigned_tickets= CASE
WHEN a.ticket_status!='0' THEN (b.assigned_tickets-1)
ELSE b.assigned_tickets END ,a.ticket_status='0'
WHERE a.enc_id=? AND b.id=a.operator_id
}
else{
UPDATE ".$SupportTicketsTable." a ,".$SupportUserTable." b
SET a.title=? , a.priority=?, a.ticket_status=?
ELSE b.assigned_tickets END ,a.ticket_status='0'
WHERE a.enc_id=?
}
When a.operator_id=0
there isn't a value for b.id
that will match the condition Is there a way to do this in only one query? 当
a.operator_id=0
,没有与条件匹配的b.id
值是否只有一种查询可以做到这一点? Thanks in advance 提前致谢
I think that your branch ELSE is wrong: 我认为您的分支ELSE是错误的:
... else{ UPDATE ".$SupportTicketsTable." a ,".$SupportUserTable." b SET a.title=? , a.priority=?, a.ticket_status=? ELSE b.assigned_tickets END ,a.ticket_status='0' WHERE a.enc_id=? }
You can create one or two srored procedures with a dynamicSQL inside and put tablenames as a parameter into there. 您可以在内部创建一两个动态SQL的存储过程,并将表名作为参数放入其中。 for example:
例如:
DELIMITER $$ DROP PROCEDURE IF EXISTS my_proc $$ CREATE PROCEDURE my_proc (IN SupportTicketsTable VARCHAR(20), IN SupportUserTable VARCHAR(20)) BEGIN SET @SQLS = CONCAT('UPDATE ',SupportTicketsTable, 'a ', SupportUserTable,' b SET a.title=? , a.priority=?, a.ticket_status=?, b.solved_tickets= CASE WHEN a.ticket_status!='0' THEN (b.solved_tickets+1) ELSE b.solved_tickets END , b.assigned_tickets= CASE WHEN a.ticket_status!='0' THEN (b.assigned_tickets-1) ELSE b.assigned_tickets END , a.ticket_status='0' WHERE a.enc_id=? AND b.id=a.operator_id'; PREPARE STMT FROM @SQLS; EXECUTE STMT; DEALLOCATE PREPARE STMT; END$$
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.