日期:2014-05-16  浏览次数:20659 次

求各位大大帮帮忙 -.- 数据库的三张表关联查询问题
content表,tag表,user表
content表中存有
c_uid--->存储user表中的id字段
c_ttype--->存储tag表中的id
c_ttag---->存储tag表中的id
c_content--->内容,无关

tag表中有
t_id--->id字段
t_tag---->tag字段,存储type或者tag

user表中有
u_id--->id字段
u_name--->用户名

 我现在需要一条查询语句实现查询content表, 其中的c_uid,c_ttype,c_ttag三个字段的值来自tag表和user表,感谢各位大大了 ...
------解决方案--------------------
左联接呗~~left join
注意的是tag表要连两次,然后用别名区分!


select * from 
content a left join tag b on  a.c_ttype=b.id
left join tag  c on  a.c_ttag=c.id
left join user d on   a.c_uid=d.id




------解决方案--------------------


 select c.c_uid,
u.u_name,
c.c_ttype,
t1.t_tag,
c.c_ttag,
t2.t_tag,
c.c_content
 from content c
 inner join [user] u
 on c.c_uid=u.u_id
 inner join tag t1
 on c.c_ttype=t1.t_id
 inner join tag t2
 on c.c_ttag=t2.t_id