最新下载
热门教程
- 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
相关文章
- 云星穹铁道权益升级怎么玩 云崩铁权益升级活动介绍 12-03
- 以闪亮之名山中绘梦怎么玩 以闪亮之名滢然花梦套装活动介绍 12-03
- 以闪亮之名绿野梦歌礼包怎么样 以闪亮之名绿野梦歌拍照礼包介绍 12-03
- 光遇12.6有什么更新 光遇12月6日更新内容介绍 12-03
- 伍六七暗影交锋测试资格怎么获得 伍六七暗影交锋内测资格获取方法 12-03
- 无限暖暖晶莹诗集怎么样 12-03