左连接和右连接的sql语句

select ro.id roleId,ro.role_name roleName,funlist.id funId,
funlist.fun_name funName,funlist.fun_parent_id funParentId,
rfmap.allow_manager allowManager,
case when ro.id is null then 0 else 1
end as ischecked
from base_role_fun_map rfmap
inner join
BASE_ROLE_LIST
ro on ro.id=rfmap.role_id and ro.id=20
right join
base_fun_list
funlist on funlist.id=rfmap.fun_id

中间显示BASE_ROLE_FUN_MAP表中的所有数据

左边显示BASE_ROLE_LIST表中ROLE_ID对应BASE_ROLE_FUN_MAP表中ROLE_ID的所有数据,没有对应数据的则显示为null

右边显示BASE_FUN_LIST表中FUN_ID对应BASE_ROLE_FUN_MAP表中FUN_ID的所有数据,没有对应数据的则显示为null

例子:

  a表     id   name     b表     id   job   parent_id   
              1   张3                   1     23     1   
              2   李四                 2     34     2   
              3   王武                 3     34     4       
  a.id同b.parent_id   存在关系   
--------------------------------------------------    
 1) 内连接   
  select   a.*,b.*   from   a   inner   join   b     on   a.id=b.parent_id       
  结果是     
  1   张3                   1     23     1   
  2   李四                  2     34     2   

  2)左连接   
  select   a.*,b.*   from   a   left   join   b     on   a.id=b.parent_id       
  结果是     
  1   张3                   1     23     1   
  2   李四                  2     34     2   
  3   王武                  null   

 3) 右连接   
  select   a.*,b.*   from   a   right   join   b     on   a.id=b.parent_id       
  结果是     
  1   张3                   1     23     1   
  2   李四                  2     34     2   
  null                       3     34     4   

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值