Re: WHERE ... IN condition and multiple columns in subquery

From: Alban Hertroys <haramrae(at)gmail(dot)com>
To: Alexander Farber <alexander(dot)farber(at)gmail(dot)com>
Cc: pgsql-general <pgsql-general(at)postgresql(dot)org>
Subject: Re: WHERE ... IN condition and multiple columns in subquery
Date: 2016-10-28 15:47:55
Message-ID: CAF-3MvN8iUxxfejVNwhNvtuWT4Cx5d5MwRB8sDwwJpkC8QOfXA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 28 October 2016 at 13:03, Alexander Farber
<alexander(dot)farber(at)gmail(dot)com> wrote:
> Hello,
>
> is it please possible to rewrite the SQL query
>
> SELECT DISTINCT ON (uid)
> uid,
> female,
> given,
> photo,
> place
> FROM words_social
> WHERE uid IN (SELECT player1 FROM games)
> OR uid IN (SELECT player2 FROM games)
> ORDER BY uid, stamp DESC
>
> where first column player1 is fetched in a subquery and then column player2
> is fetched from the same table?
>
> I've searched around and it seems that a JOIN should be used here, but can
> not figure out exactly how.
>
> Thank you
> Alex

You mean like this?:

SELECT DISTINCT ON (uid)
uid,
female,
given,
photo,
place
FROM words_social
JOIN games ON uid IN (player1, player2)
ORDER BY uid, stamp DESC

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Kim Rose Carlsen 2016-10-28 16:29:26 How to hint 2 coulms IS NOT DISTINCT FROM each other
Previous Message Mick GRIFFIN 2016-10-28 15:08:39 Cannot access the return value of a PostgreSQL function using ADO