Is there a way to include and exclude within a join? This is the current join in the query:
INNER JOIN linked_tags
AS active ON active.track_id = music.id
AND active.tag_id IN (19,25)
and I want to do this, but it’s not producing the results that I want
INNER JOIN linked_tags
AS active ON active.track_id = music.id
AND active.tag_id IN (19,25) AND active.tag_id NOT IN (44,15)
So, I want it to select any items with a tag_id of 19, but not if any other tracks exist that also have tag_id’s of 44 or 15.
As reference, here’s the whole query:
SELECT music.id, music.name AS name, music.filename, music.url_name, music.file_path_high, music.filesize, music.categories, music.duration, music.folder, tags.tag_name, music.has_versions, music.version_search_term, SUM(active.weight) AS total_weight
FROM (music)
INNER JOIN linked_tags AS active ON active.track_id = music.id AND active.tag_id IN (19,25) AND active.tag_id NOT IN (44,15)
INNER JOIN tags ON active.tag_id = tags.id
WHERE is_version = 0
OR is_version IS NULL
GROUP BY music.id
HAVING COUNT(DISTINCT active.tag_id)=1
ORDER BY total_weight DESC
LIMIT 25
If I understand your requirement correctly, then it’s not possible like that. conditions in the join are applied on the row being checked. they don’t refer to other rows in the table (not until their turn to be checked arrives). even though the same track can have multiple tags, those will be registered multiple rows in the table “linked_tags”.
if you want to select tracks with tags 19 and 25 under the condition that those same tracks don’t also have tags 44,15 then you can probably do the following: