面试题

1、利用case when进行子查询

 SELECT u.id user_id,u.username user_username,g.id group_id,
  CASE  g.flag  
  WHEN   '0'   THEN   g.groupcode  
  ELSE  (SELECT t.fullname FROM tab_0009 t WHERE t.id = g.id)
  END AS   group_groupname, 
 CASE  g.flag  
  WHEN   '0'   THEN   g.groupname  
  ELSE  (SELECT t.fullcode FROM tab_0009 t WHERE t.id = g.id)
  END AS   group_groupcode, 
u_g.id usergroup_id,u_g.user_id usergroup_userId,u_g.group_id usergroup_groupId
FROM user_group u_g RIGHT JOIN  t_group g ON u_g.group_id = g.id LEFT JOIN t_user u ON u_g.user_id = u.id  
2、将t_group和tab_0009作外连接,最后字段拼接
SELECT u.id user_id,u.username user_username,g.id group_id,
CONCAT(IFNULL(g.groupname,''),IFNULL(t.fullname,''))  group_groupname,
CONCAT(IFNULL(g.groupcode,''),IFNULL(t.fullcode,''))  group_groupcode,
u_g.id usergroup_id,u_g.user_id usergroup_userId,u_g.group_id usergroup_groupId
FROM user_group u_g ,t_user u,t_group g LEFT JOIN tab_0009 t ON t.id = g.id   WHERE u_g.group_id = g.id AND  u_g.user_id = u.id;

SELECT u.id user_id,u.username user_username,g.id group_id,
CONCAT(IFNULL(g1.groupname,''),IFNULL(t.fullname,''))  group_groupname,
CONCAT(IFNULL(g1.groupcode,''),IFNULL(t.fullcode,''))  group_groupcode,
u_g.id usergroup_id,u_g.user_id usergroup_userId,u_g.group_id usergroup_groupId

FROM user_group u_g RIGHT JOIN  t_group g ON u_g.group_id = g.id LEFT JOIN t_user u ON u_g.user_id = u.id,
t_group g1 LEFT JOIN tab_0009 t ON t.id = g1.id WHERE g1.id = g.id; 

SELECT u.id user_id,u.username user_username,g.id group_id,
g.groupname group_groupname,
g.groupcode group_groupcode,
u_g.id usergroup_id,u_g.user_id usergroup_userId,u_g.group_id usergroup_groupId
FROM user_group u_g RIGHT JOIN  t_group g ON u_g.group_id = g.id LEFT JOIN t_user u ON u_g.user_id = u.id 
 WHERE g.flag != 0 
 
UNION 
 SELECT u.id user_id,u.username user_username,g.id group_id,
(SELECT t.fullname FROM tab_0009 t WHERE t.id = g.id)  group_groupname,
(SELECT t.fullcode FROM tab_0009 t WHERE t.id = g.id) group_groupcode,
u_g.id usergroup_id,u_g.user_id usergroup_userId,u_g.group_id usergroup_groupId
FROM user_group u_g RIGHT JOIN  t_group g ON u_g.group_id = g.id LEFT JOIN t_user u ON u_g.user_id = u.id 
 WHERE g.flag = 0 



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值