Re: Extend inner join to fetch not yet connected rows also - Mailing list pgsql-general

From Jan Kohnert
Subject Re: Extend inner join to fetch not yet connected rows also
Date
Msg-id 9177656.fJROvCpnoI@kohni-mobil
Whole thread Raw
In response to Re: Extend inner join to fetch not yet connected rows also  (Arup Rakshit <ar@zeit.io>)
Responses Re: Extend inner join to fetch not yet connected rows also
List pgsql-general
Hi Arup,

Am Sonntag, 22. September 2019, 15:30:38 CEST schrieb Arup Rakshit:
> When someone adds a craftsmanship to their skill set, the contractor_skills
> table holds that relationship. I don’t think distinct is the correct tool,
> as it will eliminate the correct data. users and craftsmanship has m:n
> relationship via the join table contractor_skills.

depending on the definition of table "contractor_skills" it can give you a n:m
relationship between user_id and craftmanship_id, that is true.

> SELECT
>     craftsmanships.id,
>     craftsmanships.name,
>     CASE WHEN contractor_skills.user_id IS NULL THEN
>         FALSE
>     ELSE
>         TRUE
>     END AS has
> FROM
>     "craftsmanships"
>     LEFT JOIN "contractor_skills" ON
"contractor_skills"."craftsmanship_id" =
> "craftsmanships"."id" LEFT JOIN "users" ON "users"."id" =
> "contractor_skills"."user_id" WHERE (contractor_skills.user_id = 8
>     OR contractor_skills.user_id IS NULL)
> ORDER BY
>     "craftsmanships"."id”;

BUT: you don't use any of users' columns in select, where, or order by. And
since users is in a left join it is just a table which is neither used nor
relevant in that particular statement.

In the end, it depends on how data is structured in your database and what you
want to achieve.

--
MfG Jan





pgsql-general by date:

Previous
From: John W Higgins
Date:
Subject: Re: Extend inner join to fetch not yet connected rows also
Next
From: Thiemo Kellner
Date:
Subject: Re: Use of ?get diagnostics'?