我有两个表:
表1(客户)
customer_id customer_name salesPerson_id
1 John 1
2 Ed 1
3 Sam 2
表2(客户联系人)
contact_id customer_id phone_number
1 1 687-5309
2 1 555-1234
3 1 742-1111
我试图仅让销售人员为其特定客户添加/更新电话号码。
因此,只有salessPerson_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;
这是您应该以本机方式使用的查询,但是您需要在应用程序框架中使其适合
关于mysql - 如果第二个表具有条件值,则插入到表中,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51924031/