goqu icon indicating copy to clipboard operation
goqu copied to clipboard

PostgreSQL. DELETE FROM ... USING

Open inpos opened this issue 2 years ago • 2 comments

Hi! I need to build following query:

DELETE FROM table1 t1 USING (SELECT id FROM table2) AS t2 WHERE t1.tab2_id = t2.id

But DeleteDataset does not have method Using. How can I achieve my goal?

inpos avatar Aug 24 '22 06:08 inpos

The same query could also be expressed as:

DELETE FROM table1 t1 WHERE t1.id IN (SELECT id from table2)

abdusco avatar Sep 30 '22 16:09 abdusco

Sorry to bump, but I also am interested with how to use the USING feature of Postgres. @abdusco your example does not help the case where you want to use a column from the other table in the RETURNING clause.

Example: https://dbfiddle.uk/spBZMg5R

DELETE FROM a 
USING b 
WHERE a.cluster = b.cluster AND a.cluster = 10 
RETURNING a.id, b.val;

nirhaas avatar Nov 20 '23 18:11 nirhaas