4表查询问题

artak 2007-11-26 09:27:04
select [user].userName,shop.shopname,role.rolename from [user],shop,userrole,role where [user].userid='qq'
and [user].shopid = shop.shopid and userrole.roleid=role.roleid and [user].userid=userrole.userid
上面的语句是4表查询,目的是将userName,shopName,rolename选择出来,还有其他办法吗?听过有个上面join
比如
select [user].userName,shop.shopnamefrom [user]
join shop on [user].userid='qq' and [user].shopid = shop.shopid
可是我不懂怎么连接3个以上的表
...全文
55 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
artak 2007-11-26
  • 打赏
  • 举报
回复
用left join 会有空值,用join就没有了
Limpire 2007-11-26
  • 打赏
  • 举报
回复
select [user].userName, shop.shopname, role.rolename from [user]
join shop on [user].shopid=shop.shopid
join userrole on [user].userid=userrole.userid
join role on userrole.roleid=role.roleid
--join another table
where [user].userid='qq'
dawugui 2007-11-26
  • 打赏
  • 举报
回复
你的方法要快过左连接left join
四表连接方法如下:

select a.*,b.*,c.*,d.* from a 
left join b on a.id = b.id
left join c on a.id = c.id
left join d on a.id = d.id
and a.userid='qq'

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧