views:

130

answers:

3

I have two tables: photographs, and photograph_tags. Photograph_tags contains a column called photograph_id (id in photographs). You can have many tags for one photograph. I have a photograph row related to three tags: boy, stream, and water. However, running the following query returns 0 rows

SELECT p.*
FROM photographs p, photograph_tags c
WHERE c.photograph_id = p.id
AND (c.value IN ('dog', 'water', 'stream'))
GROUP BY p.id
HAVING COUNT( p.id )=3

Is something wrong with this query?

My tables look like so
-----------------------
photographs
-----------------------
id | title | location
------------------------
7  | asdf | c:\...


-----------------------
photograph_tags
-----------------------
id | photograph_id | value
1  | 7             | dog
2  | 7             | water
3  | 7             | stream
4  | 7             | mountains

I want to pull all photograph rows that relate to at least all three of the searched tags
+1  A: 

to get all photos with the 3 tags (OR MORE) you specified. Start with the Tags and join the photos.

select
 p.id
from photographs p

left join photograph_tags c
on p.id = c.photograph_id
and c.value IN ('dog', 'water', 'stream')

group by p.id

having count(c.value) >= 3

testing the above code:

create table #photograph_tags (
    photograph_id INT,
    value varchar(50)
)

create table #photographs (
    id int
)

insert into #photographs values (7)
insert into #photographs values (8)

insert into #photograph_tags values (7, 'dog')
insert into #photograph_tags values (7, 'water')
insert into #photograph_tags values (7, 'stream')
insert into #photograph_tags values (7, 'mountains')
insert into #photograph_tags values (8, 'stream')
insert into #photograph_tags values (8, 'mountains')

select * from #photographs
select * from #photograph_tags

select
    p.id
from #photographs p

left join #photograph_tags c
on p.id = c.photograph_id
and c.value IN ('dog', 'water', 'stream')

group by p.id

having count(c.value) >= 3


drop table #photograph_tags
drop table #photographs
Daniel Brink
This query is displaying some of the same rows multiple times
ThinkingInBits
and some that don't have all three
ThinkingInBits
@ThinkingInBits: there you go, updated the sql so the the photo must have all there tags
Daniel Brink
empty result set now... I edited the original post with my basic db schema
ThinkingInBits
@ThinkingInBits: just tested it with your data, works perfectly, check that your not getting you id columns mixed up
Daniel Brink
This looks just like how my tables are set up, but I'm not getting any results
ThinkingInBits
@ThinkingInBits: strange as I am definitely getting a result. maybe there isn't a photo with the 3 specific tags in you database. good luck, i've got to get back to work
Daniel Brink
There are two images that contain all three tags and more
ThinkingInBits
Figured out the problem... had to add having count(c.value) >= 3... Thanks! I need to brush up on my SQL :D
ThinkingInBits
@ThinkingInBits: awesome :) updated my answer to include the >=3
Daniel Brink
A: 
SELECT p.* FROM photographs p join 
(select id, COUNT(id) as TagCount 
    FROM Photograph_Tags c
    WHERE c.value IN ('dog', 'water', 'stream')
    group by id) 
    as TagCounts on p.id = TagCounts.id
WHERE TagCount = 3
DancesWithBamboo
A: 

SELECT p.* FROM photographs p WHERE (c.value IN ('dog', 'water', 'stream')) AND (SELECT COUNT(*) FROM photograph_tags c
WHERE c.photograph_id = p.id) >= 3;

will give you photographs with at least three tags.

Turin