左连接和右连接的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   

猜你喜欢

转载自blog.csdn.net/weixin_40620337/article/details/80638795