mysql - 如何在 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;
解决方案
推荐阅读
- python - Python:对于范围无法正常工作,因为我计划使用替换功能
- google-sheets - 如何根据类似 SQL 的查询更新谷歌表格值?
- php - 为什么 vue 组件说数据中没有变量?
- android - android-studio:格式化对话框插入未知字符
- python - LSTM 模型的输入顺序是否会影响 model.predict?
- pandas - 线性展开 pandas 中的度数
- hyperledger-fabric - 未能创建新连接:desc = 传输:拨号时出错:拨号 tcp 172.19.0.4:9051:连接:连接被拒绝 Hyperledger Fabric
- javascript - JavaScript 返回 false 但仍在提交
- c# - ID #### 的进程未运行
- robotframework - 我的以下代码有替代方法吗?