views:

269

answers:

4

I have two tables in my database, one contains a list of items with other information on these items. The other table is contains a list of photographs of these items.

The items table gives each item a unique identifier,which is used in the photographs table to identifier which item has been photographed.

I need to output a list of items that are not linked to a photograph in the second table. Any ideas on how I can do this?

A: 
SELECT * FROM items WHERE id NOT IN (SELECT item_id FROM photos);

should be what you want

oedo
oedo, thanks for the response
ee12csvt
+2  A: 
select i.*
from Items i
left outer join Photographs p on i.ID = p.ItemID
where p.ItemID is null
RedFilter
Only answer to use joins instead of inner select statements!! Please pick this answer as joins are much easier to read, better sql, and preform much better then inner selects
george9170
Orbman,Thanks for thew quick response, I have adapted the code for my task and it works fine.I must read up on JOINS, I am quite new to writing my own queries, but they do seem to make it alot easier.Once again Thanks :)
ee12csvt
@ee12csvt: So you need to award the correct answer to RedFilter. This is how people reading you question know the correct answer.
Jonathan
A: 

use distinct if one item has more than one photograph.

SELECT * FROM items WHERE id NOT IN (SELECT distinct(item_id) FROM photos);
Salil
Salil, thanks for the response
ee12csvt
A: 
SELECT id,name from tbl_item 
WHERE id NOT IN (SELECT distinct(tbl_item.id) FROM tbl_item INNER JOIN tbl_photo ON tbl_photo.pid=tbl_item.id)
nik
nik, thanks for the response
ee12csvt