繁体   English   中英

如果第二张表具有条件值,则插入到表中

[英]Insert into table if second table has conditional value

我有两个表:

表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;

这是您应该以本机方式使用的查询,但是您需要在应用程序框架中使其适合

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM