mysql查询多个表

时间:2013-11-03 21:59:07

标签: mysql

我有3个三个表如下,用户可以拥有多个联系人,许多用户都可以列出联系人......

//user table

user_id | username|password|fname|lname|email|contactnumber

//contact table

contact_id | fname | lname |email|contactnumber

//user_contact table

user_id |contact id | relationship type |relationship state

我的查询必须显示链接到所选用户的所有联系人...任何建议都会有帮助

所以它看起来像这样

结果:

user fname | user lname | email address | contact number of user | contact first name | contact last name | relationship type | relationship state  

1 个答案:

答案 0 :(得分:0)

如果我理解你的问题错误,请纠正我: 那么用户表和联系表有多对多关系? 那么你可以做到

select u.fname,u.lname,u.email,u.contactnumber,c.fname,c.lname,uc.relationship_type,uc.relationship_state 
from user as u
inner join user_contact as uc on u.user_id=uc.user_id
inner join contact as c on uc.contact_id=c.contact_id
where u.user_id=<userId>
相关问题