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

From: Geoff Winkless <pgsqladmin(at)geoff(dot)dj>
To: Alexander Farber <alexander(dot)farber(at)gmail(dot)com>, Postgres General <pgsql-general(at)postgresql(dot)org>
Subject: Re: WHERE ... IN condition and multiple columns in subquery
Date: 2016-10-28 14:31:58
Message-ID: CAEzk6feSkerworOgr4e2tVdJ7M9uGKJWBQ4rWGeCBGqP7+7y6Q@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 28 October 2016 at 12:03, Alexander Farber <alexander(dot)farber(at)gmail(dot)com>
wrote:

> 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?
>

You could use

​WHERE EXISTS (SELECT FROM games WHERE player1=uid OR player2=uid)

although as Tom says, it's dubious whether that will result in a
significant speedup.

Geoff​

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Mick GRIFFIN 2016-10-28 15:08:39 Cannot access the return value of a PostgreSQL function using ADO
Previous Message Tom Lane 2016-10-28 14:25:31 Re: deadlock error - version 8.4 on CentOS 6