SQL Select rows that do not have an association to a corresponding join table

robskrob :

Hi I have a SQL question. I am trying to get the rows of posts that do not have a corresponding s3 bucket row in another table. Here is the query I have which will get me the posts that have an associated row in the s3 bucket table.

SELECT * FROM wp_posts
INNER JOIN wp_as3cf_items
ON wp_as3cf_items.source_id = wp_posts.id
WHERE wp_posts.post_type = 'attachment';

What SQL can I write that will return the wp_posts rows that are of post_type attachment that do not have a an associate s3 row?

Gordon Linoff :

Use not exists:

SELECT p.* 
FROM wp_posts p
WHERE NOT EXISTS (SELECT 1
                  FROM wp_as3cf_items i
                  WHERE i.source_id = p.id
                 ) AND
      p.post_type = 'attachment';

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=372276&siteId=1