Selecting similar videos except for current one using MySQL

84 Views Asked by At

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.

2

There are 2 best solutions below

13
On BEST ANSWER

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';
0
On

(SELECT video_id FROM videos WHERE video_id = '$CurrentVideo'). That subquery returns the same output as it's input. You already have video ID; you don't need to select it again. Have you tried just an inequality operator there? Furthermore, you're comparing it to itself, aren't you? Try this instead:

video_id <>'$CurrentVideo'