Search code examples
mysqlmariadbexcept

Selecting similar videos except for current one using MySQL


I have a table with videos' info and when someone selects a video on my site I have a small section that shows all other videos with similar tags. Now, I want to exclude the video that is currently selected. Here's my query:

SELECT video_title, video_desc
FROM videos WHERE tags LIKE 
$CurrentTags AND   
'$CurrentVideo' NOT IN
(SELECT video_id FROM videos WHERE video_id = '$CurrentVideo')

The query works fine (as in there are no errors) but it sometimes still returns me a video that is already selected. I think it's obvious but I'll clarify anyway

$CurrentTags is a set of tags of currently selected video on the website.

$CurrentVideo is a unique video id of currently selected video on website.


Solution

  • This must be the same as your quer without a subselect:

    SELECT video_title, video_desc
    FROM videos 
    WHERE (
        tags LIKE $CurrentTags
    OR
        tags LIKE $otherTags
    OR
        tags LIKE $otherTags
    )
    AND   
        video_id <> '$CurrentVideo';