最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
postgresql多表join方法的用法
时间:2022-06-29 10:22:26 编辑:袖梨 来源:一聚教程网
user_info要关联查出其它社交表里的信息,但是其它社交表可能没有这个用户
select u.*, COALESCE(u.slogan, tw.description, i.bio, g.bio,tu.description) as bio from user_info u LEFT OUTER JOIN twitter_user tw ON u.user_name = tw.screen_name LEFT OUTER JOIN instagram_user i ON u.user_name = i.username LEFT OUTER JOIN github_user g ON u.user_name = g.login LEFT OUTER JOIN tumblr_user tu ON u.user_name = g.name
引出了另外一个问题:postgresql应如何判断空字符串
postgresql多表join 中用了 COALESCE
但是空的string还是会被选出来''
得再加个NULLIF判断来解决
select u.*, COALESCE(NULLIF(u.slogan,''), NULLIF(tw.description,''), NULLIF(i.bio,''), NULLIF(g.bio,''), NULLIF(tu.description,'')) as bio from user_info u LEFT OUTER JOIN twitter_user tw ON u.user_name = tw.screen_name LEFT OUTER JOIN instagram_user i ON u.user_name = i.username LEFT OUTER JOIN github_user g ON u.user_name = g.login LEFT OUTER JOIN tumblr_user tu ON u.user_name = g.name
于是改成了这样
select u.*, COALESCE(NULLIF(u.slogan,''), NULLIF(tw.description,''), NULLIF(i.bio,''), NULLIF(g.bio,''), NULLIF(tu.description,'')) as bio from user_info u LEFT OUTER JOIN twitter_user tw ON u.user_name = tw.screen_name LEFT OUTER JOIN instagram_user i ON u.user_name = i.username LEFT OUTER JOIN github_user g ON u.user_name = g.login LEFT OUTER JOIN tumblr_user tu ON u.user_name = g.name
相关文章
- 《彩色点点战争》推图常用三大主c玩法详解 01-23
- 《燕云十六声》池鱼林木任务攻略 01-23
- 《大连地铁e出行》查看行程记录方法 01-23
- 《明日方舟》2025春节限定干员余角色介绍 01-23
- 《崩坏:星穹铁道》万敌光锥搭配攻略 01-23
- 《燕云十六声》一药千金任务攻略 01-23