Обсуждение: [GENERAL] Coditional join of query using PostgreSQL

Поиск
Список
Период
Сортировка

[GENERAL] Coditional join of query using PostgreSQL

От
Nick Dro
Дата:
Hi,
I'm new to postgresql and couldn't find answer to this situation anywhere.
I asked this here:
http://stackoverflow.com/questions/43984208/coditional-join-of-query-using-postgresql

I hope there is a better solution rather than creating two separated functions :(

Re: [GENERAL] Coditional join of query using PostgreSQL

От
"David G. Johnston"
Дата:
On Mon, May 15, 2017 at 10:02 AM, Nick Dro <postgresql@walla.co.il> wrote:
Hi,
I'm new to postgresql and couldn't find answer to this situation anywhere.
I asked this here:
http://stackoverflow.com/questions/43984208/coditional-join-of-query-using-postgresql

I hope there is a better solution rather than creating two separated functions :(

​Generate the SQL itself in a string then execute the string.  Its called "Dynamic SQL"​.


David J.

Re: [GENERAL] Coditional join of query using PostgreSQL

От
Jack
Дата:
This is a join in a middle of query.
How can I do such thing?



--
View this message in context:
http://www.postgresql-archive.org/Coditional-join-of-query-using-PostgreSQL-tp5961718p5961726.html
Sent from the PostgreSQL - general mailing list archive at Nabble.com.


Re: [GENERAL] Coditional join of query using PostgreSQL

От
Nick Dro
Дата:
This is a join in a middle of query.


How can I use dynamic SQL in the middle of query?

ב מאי 15, 2017 20:26, David G. Johnston כתב:
On Mon, May 15, 2017 at 10:02 AM, Nick Dro <postgresql@walla.co.il> wrote:
Hi,
I'm new to postgresql and couldn't find answer to this situation anywhere.
I asked this here:
http://stackoverflow.com/questions/43984208/coditional-join-of-query-using-postgresql

I hope there is a better solution rather than creating two separated functions :(

​Generate the SQL itself in a string then execute the string.  Its called "Dynamic SQL"​.


David J.



Re: [GENERAL] Coditional join of query using PostgreSQL

От
Alban Hertroys
Дата:
> On 15 May 2017, at 19:02, Nick Dro <postgresql@walla.co.il> wrote:
>
> Hi,
> I'm new to postgresql and couldn't find answer to this situation anywhere.
> I asked this here:
> http://stackoverflow.com/questions/43984208/coditional-join-of-query-using-postgresql
>
> I hope there is a better solution rather than creating two separated functions :(

You can use your boolean parameter inside the join condition:
[…] on (tfquery.a = main.a and ((type_f and tfquery.d = main.d) or not type_f))

Beware that you don't also have a column named type_f in that join somewhere.

Alban Hertroys
--
If you can't see the forest for the trees,
cut the trees and you'll find there is no forest.