mysql - 如果第二个表具有条件值,则插入表中
问题描述
我有两张桌子:
表1(客户)
customer_id customer_name salesPerson_id
1 John 1
2 Ed 1
3 Sam 2
表 2 (customerContacts)
contact_id customer_id phone_number
1 1 687-5309
2 1 555-1234
3 1 742-1111
我试图只让销售人员为其特定客户添加/更新电话号码。
所以只有 sales salesPerson_id 1 可以更新 John 和 Ed,只有 salesPerson_id 2 可以更新 Sam。
我相信我正在寻找类似的东西:
INSERT INTO customerContacts (contact_id , customer_id , phone_number) VALUES (1 , 1 , '987-6543')
ON DUPLICATE KEY UPDATE phone_number='987-6543'
IF customers.salesPerson_ID = 1
但似乎 sql 不支持 if 语句。
解决方案
INSERT INTO customerContacts (contact_id , customer_id , phone_number)
Select 1 , customer_id , '987-6543'
from customers
where salesPerson_ID = 1 and customer_id=1;
这是您应该以本机方式使用的查询,但您需要将其放入您的应用程序框架中
推荐阅读
- swift - SwiftUI - 使用 CoreData 保存图像
- python - numpy.float64' 对象不可调用 - 超参数调整
- html - 如何使用 Vue 的 String 和后端数据显示图像?
- sql - 从登录时间和注销时间为不同行的表中计算持续时间
- google-apps-script - 在基于应用程序脚本的邮件合并中附加多个文件(动态和静态)
- c++ - 将原始指针与其拥有的 shared_ptr 一起缓存以获得更好的访问性能是一个好主意吗?
- flutter - 在 null 上调用了 getter 'documents'
- r - Rmarkdown 无法识别 kable_styling 命令中的内联
- python - HERE 请求如何为 params 中的键分配多个值
- node.js - 有没有办法忽略 Node.JS tls 中的自定义 CA 公用名?