0

我正在尝试问题中写的内容,可能是一个愚蠢的问题,但我找不到解决方案,基本上这就是我想做的:

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=?
}

a.operator_id=0没有b.id匹配条件的值时有没有办法只在一个查询中做到这一点?提前致谢

4

1 回答 1

1
  1. 我认为您的分支 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=?
    }
    
  2. 您可以创建一个或两个带有动态 SQL 的 srored 过程,并将表名作为参数放入其中。例如:

    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$$
    
于 2013-06-07T10:18:22.763 回答