Re: Displaying chat by punished users only to themselves (db fiddle attached)

From: Alexander Farber <alexander(dot)farber(at)gmail(dot)com>
To:
Cc: pgsql-general <pgsql-general(at)postgresql(dot)org>
Subject: Re: Displaying chat by punished users only to themselves (db fiddle attached)
Date: 2022-05-04 17:16:08
Message-ID: CAADeyWj8t4ByvQiyNs2kkbWyxccecRoZax+O_Hihs=ZxGEmoMw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

I think I am very close with the following CTE, but do not understand how
to bring it into the main SELECT query:

https://dbfiddle.uk/?rdbms=postgres_14&fiddle=ee264dc98b44dee75aa4523164a327b3

WITH myself AS (
SELECT uid
FROM words_social
WHERE social = in_social
AND sid = in_sid
LIMIT 1
)
SELECT
CASE WHEN c.uid = myself.uid THEN 1 ELSE 0 END,
c.msg
FROM words_chat c
JOIN words_games g USING (gid)
JOIN words_users opponent ON (opponent.uid IN (g.player1,
g.player2) AND opponent.uid <> myself.uid)
WHERE c.gid = in_gid
-- always show myself my own chat messages
AND c.uid = myself.uid
-- otherwise only show messages by not muted opponents
OR NOT opponent.muted
ORDER BY c.CREATED ASC;

The error message is:

ERROR: missing FROM-clause entry for table "myself"
LINE 64: ...uid IN (g.player1, g.player2) AND opponent.uid <> myself.uid...
^

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Francisco Olarte 2022-05-04 17:19:29 Re: Backing up a DB excluding certain tables
Previous Message JORGE MALDONADO 2022-05-04 17:12:38 Re: Backing up a DB excluding certain tables