I am completely stuck on this and would appreciate help from someone who is more fluent in sql than me!
Situation: I have Images which can belong to up to 3 Categories. I have to look them up using the category name (e.g. 'School'). I have an Image table, a Category table, and a CategoryImage table as a join table
The essential fields are
Category
Id
categoryname
Image
Id
imagename
description
CategoryImage
categoryId
imageId
If Image '001' belongs to the categories called 'School' and 'Home' it will have 2 entries in CategoryImage.
I need the sql to find all images which are in BOTH the 'School' AND the 'Home' category. I can see that this sql is probably trying to return a single image where the category is 'School AND 'Home' which is clearly impossible. [ Changing the AND to an OR finds images which be log to the 'School' category plus also images which belong to the 'Home' category, which is not what I need.
SELECT
DISTINCT t0.description,
t0.imagename
FROM
Image t0, Category T2, CategoryImage T1
WHERE
(T2.name = "School"
AND T2.name = "Home")
AND T1.categoryId = T2.id
AND t0.id = T1.imageId
Thanks in advance for any suggestions.