The usage of PostgreSQL join method with multiple tables

Source: Internet
Author: User
Tags join postgresql


User_info to correlate information from other social tables, but other social forms may not have this user

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

Raises another question: how PostgreSQL should judge an empty string

PostgreSQL coalesce in multiple table joins

But an empty string will still be chosen.

We're going to have to add a nullif judgment.

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 INS  Tagram_user i on
u.user_name = I.username left
OUTER JOIN github_user g in
u.user_name = G.login
Left OUTER JOIN tumblr_user tu on
u.user_name = G.name


and changed to this.

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
  

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.