0

Im getting the following Error on my query:

ERROR:  syntax error at or near "p"

This is my query:

  DELETE p FROM property_import_image_results p
  LEFT JOIN LATERAL (SELECT q.created_at FROM property_import_image_results q
  WHERE q.external_url=p.external_url AND (q.listing_image_id = p.listing_image_id OR q.listing_image_id IS NULL)
  ORDER BY q.created_at DESC NULLS LAST LIMIT 1) as qf ON p.created_at = qf.created_at
  WHERE qf.created_at is NULL;

Any idea what might be the problem?

3
  • postgres. I'll fix the question now. Commented Dec 1, 2015 at 15:38
  • DELETE FROM property_import_image_results p Commented Dec 1, 2015 at 15:39
  • You can't use left join like that in a DELETE statement: postgresql.org/docs/current/static/sql-delete.html Commented Dec 1, 2015 at 15:42

3 Answers 3

2

You cannot phrase your query this way. Perhaps this is what you want:

DELETE FROM property_import_image_results p
    WHERE NOT EXISTS (SELECT 1
                      FROM property_import_image_results q
                      WHERE q.external_url = p.external_url AND
                            q.created_at > p.created_at AND 
                            (q.listing_image_id = p.listing_image_id OR  q.listing_image_id IS NULL)
                     ) ;
Sign up to request clarification or add additional context in comments.

2 Comments

thanks @Gordon Linoff but I need the ORDER BY in order to get the last created result (im basically trying to get avary result, group by external_url, except the most recent one)
@ntonnelier . . . I see. I changed the = to a >, which should get the most recent version for updating.
0

There is no need of p between DELETE and FROM clause : http://www.w3schools.com/sql/sql_delete.asp. So try to delete it.

3 Comments

meta.stackexchange.com/questions/87678/… - probably professional rivalry, but just sayin'
Not the case when you intend to delete from JOIN. This was my first attempt actually..
@Strawberry thanks for the reference, I will use this reference to w3schools more carefully in the future.
0

While I'm not familiar with postgresql's specifics, aren't you essentially saying "delete mytable from mytable" in the beginning of your delete statement? Shouldn't that be "delete from property_import_image_results p ...etc. etc."?

1 Comment

Your answers will generally be better received if they are not phrased as questions.

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.