2

So it doesn't seem like there are DELETE JOINs in Postgres so I tried using USING:

DELETE FROM
    creator.list_items li
USING
    creator.lists l
USING
    item_instances ii
WHERE
    li.list_id = l._id
    AND
    li.item_instance_id = ii._id
    AND
    ii.item_id IN ($1:list)
    AND
    l._id = $2
    AND
    l.account_id = $3

but it just gives me the error:

ERROR:  syntax error at or near "USING"
LINE 5:                 USING

my original query:

DELETE 
    li
FROM
    creator.list_items li
JOIN
    creator.lists l
    ON
        li.list_id = l._id
JOIN
    item_instances ii
    ON
        li.item_instance_id = ii._id
WHERE
    ii.item_id IN ($1:list)
    AND
    l._id = $2
    AND
    l.account_id = $3
3
  • The using keyword can only be used once, just like e.g. the where keyword. Commented Feb 13, 2019 at 15:11
  • 1
    Possible duplicate of PostgreSQL delete with inner join Commented Feb 13, 2019 at 15:13
  • @Jens so I've got two USINGs in my query, does that mean I just need to use more SELECT in the WHERE part? Commented Feb 13, 2019 at 15:15

2 Answers 2

2

You can actually use JOIN in the USING clause:

DELETE FROM
    creator.list_items li
USING creator.lists l JOIN
      item_instances ii
      ON li.list_id = l._id
WHERE li.item_instance_id = ii._id AND
      ii.item_id IN ($1:list) AND
      l._id = $2 AND
      l.account_id = $3;
Sign up to request clarification or add additional context in comments.

3 Comments

for that query I'm getting: HINT: There is an entry for table "li", but it cannot be referenced from this part of the query. needed to switch ii and li to get rid of the error.
hold on, I messed up, that would just delete from item_instances, so the problem still stands
@a_horse_with_no_name i made an edit before which was incorrect, which I am referring to, you can check the edit history.
0

My functioning query:

DELETE FROM
    creator.list_items li
USING 
    creator.lists l 
WHERE 
    li.item_instance_id 
        IN 
        (
            SELECT
                _id
            FROM
                creator.item_instances
            WHERE
                item_id IN ($1:list) 
        )
    AND
    li.list_id = $2 
    AND
    li.list_id = l._id
    AND
    l.account_id = $3

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.