last day (14 days later) » 

8:49 AM
1
A: Where clause not working as expected with junction table?

VinayYou need to use OR instead of AND in the where clause since you are looking for both database and q&a. so the where condition should be like WHEREtags.tag_name = 'database' OR 'q&a'

 
Thanks, but I've tried that too. It gives only rows with thedatabase tag.
 
I am not sure about MySQL but if it is similar to SQL Server, you should be using it like this WHERE tags.tag_name = 'database' OR tags.tag_name = 'q&a' check if this works
 
I honestly don't know, I've tried everything I can think of and been researching this for days but it just won't work. Seems that the results are returned with the first where and it ignores the second. It may be an issue with the JOIN and they are not related properly.
 
Well I tried to work it out on SQLFiddle and it seems to work Can you reproduce your problem here
 
Hmmm that OR is actually returning something, but it returns all the records related to only database as well as the records related to database and q&a. I need a query that returns only records related to database and q&a not the tags individually. Which is why I was using the AND operator.
 
8:49 AM
Ok now I understand your question. Try this -- SELECT tag_to_url.url_id, websites.url, tags.tag_name FROM tag_to_url` INNER JOIN (websites,tags) ON websites.url_id = tag_to_url.url_id AND tag_to_url.tag_id = tags.tag_id WHERE websites.url in ( SELECT websites.url FROM tag_to_url INNER JOIN (websites,tags) ON websites.url_id = tag_to_url.url_id AND tag_to_url.tag_id = tags.tag_id WHERE tags.tag_name = 'database' OR tags.tag_name = 'q&a' Group by websites.url having count(tags.tag_name) > 1 ORDER BY tag_to_url.url_id );`
 
Yes that is correct, I will clarify it in the question.
That's a really interesting query. It's closer but it returns all tags related to the url that is related to either of those tags. So based on my first example it returns all rows for stackexchange.com. database, q&a, and programming.
 
yes just add the where condition again in the outer query
WHERE `websites`.url in
( ... )
and tags.tag_name = 'database' OR tags.tag_name = 'q&a' ;
I feel this is not an optimized bu =t might give you further idea
 
I'll try that
Unfortunately that didn't work, it only produces database rows.
Seems strange that something like this is so difficult.
But I don't understand databases very well so that may be why
It was so much easier when I had all these columns in one table together, but that's not the correct way to do it
 
try that here
http://sqlfiddle.com/#!2/792c94/1
 
9:07 AM
Only gives me database rows.
Thanks for everything you have tried but I am giving up on it tonight and going to bed.
 

last day (14 days later) »