A query on two joined tables

join;select

I have 2 tables. The first table is feeds and (feeds.ID) is the primary key. The second table is items. feeds.ID is related to items.feed_ID, so with a simple inner join I can extract the data of each feed.

Now, I deleted some of the feeds but didn't delete them from the items table. I want to find the items which have no resource on the feeds table. On the other hand, I want to find the useless items. What is the correct query?

I have tried this:

select * from items where feed_id NOT IN 
    (select id from feeds 
        inner join items on feeds.id=items.feed_id group by feed_id) group by feed_id

but it shows an error.

Best Answer

Try this query:

select * from items where feed_id NOT IN (select id from feeds)

Also set foriegn key relationship with delete cascade. So it will get deleted from dependent tables as well.