首页 > 解决方案 > 如何在 MySQL 中使用 insert 和 update 执行 case 语句?

问题描述

我有一种情况,我需要使用“participant_search”中的数据更新表“消费者”的信息,如果该行不存在,我需要插入。有人会认为“在重复键更新时插入...”,但是键是自动递增的,因为不需要每个其他列都是唯一的。唯一性来自几列的组合。显然“如果存在”不能在 MySQL 中使用(仅限 MSSQL)。无论如何,这是我的代码。错误是“......对于'CASE WHEN......'附近的正确语法谢谢您提前指导。

SELECT * FROM consumers 
  CASE 
    WHEN tenantid = 'tenant1' and DOB = '1970-01-01' and nameLast = 'Johnson' and ptID = '123456' 
      THEN UPDATE consumers ptID=p.ID, tenantid=p.tenantid, NameLast=p.LastName, NameFirst=p.FirstName, 
        Address1=p.Address1, Address2=p.Address2, City=p.City, State=p.State, Zip=p.Zip, County=p.County, 
        DOB=p.DateofBirth 
      ELSE INSERT INTO consumers (ptID, tenantid, NameLast, NameFirst, Address1, Address2, City, State, 
        Zip, County, DOB) select p.ID, p.tenantid, p.LastName, p.FirstName, p.Address1, p.Address2, 
        p.City, p.State, p.Zip, p.County, p.DateofBirth 
  END 
INNER JOIN participant_search p;

标签: mysql

解决方案


推荐阅读