客户 电话号码 customers_has_phonenumbers
customers -> detailed
customers.customer_id int(11) primary auto_increment
customers.customer_name varchar(255)
phonenumbers -> detailed
phonenumbers.phonenumber_id int(11) primary auto_increment
phonenumbers.phonenumber_type int(11)
phonenumbers.phonenumber_name varchar(255)
phonenumbers.phonenumber varchar(15)
customers_has_phonenumbers -> detailed
customers_has_phonenumbers.customer_id int(11) -> customers_customer_id
customers_has_phonenumbers.phonenumber_id int(11) -> phonenumbers_phonenumber_id
如何插入和更新电话号码
希望你们能帮忙答案 0 :(得分:1)
请参阅this page上的多表语法。
答案 1 :(得分:1)
插入新的电话号码
insert into phonenumbers (phonenumber_type,phonenumber_name,phonenumber) values (1,'example name','example number');
insert into customers_has_phonenumbers (customers_customer_id,phonenumbers_phonenumber_id) values ((select customer_id from customers where customer_name = 'example customer name'),(select phonenumber_id from phonenumbers where phonenumber = 'example number');
更新电话号码
insert into phonenumbers (phonenumber_type,phonenumber_name,phonenumber) values (1,'example name','new phone number');
update customers_has_phonenumbers set phonenumbers_phonenumber_id = (select phonenumber_id from phonenumbers where phonenumber = 'new phone number') where phonenumbers_phonenumber_id = (select phonenumber_id from phonenumbers where phonenumber = 'old phone number') and customers_customer_id = (select customer_id from customers where customer_name = 'example customer name');
不确定如何处理拥有相同客户名称的客户!
如果合理的话,在phonenumbers.phonenumber列上也可能有一个唯一的密钥。否则,在update sub-select中应该使用任何唯一标识phonenumbers行(除了phonenumbers.phonenumber_id)来检索phonenumbers.phonenumber_id值。同一更新语句中的客户子选择也是如此。
此外,我不确定您在插入新电话号码时是否已有客户行?如果没有,那么您必须在插入到customers_has_phonenumbers表之前插入新客户行。
希望这有帮助。