customers
phonenumbers
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) -> holds customers.customer_id
customers_has_phonenumbers.phonenumber_id int(11) -> holds phonenumbers.phonenumber_id
如何通过customers.customer_id从电话号码获取数据?
我希望你们能帮助我
最佳答案
SELECT * FROM phonenumbers,customers,customers_has_phonenumbers
WHERE customers.custumer_id = customers_has_phonenumers.customer_id
AND phonenumbers.phonenumber_id = customers_has_phonenumers.phonenumber_id
AND customers.customer_id = [ID HERE]
注意:将*替换为您需要的字段,仅用于演示
更好的解决方案可能是删除两者之间的表,但是当然只有在客户拥有1个电话号码的情况下,这才有效。
关于mysql - 如何结合关系表结合2个表,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7644957/