Jump to content

JOIN .... WHERE != help


helpmeplease2

Recommended Posts

I am trying to make it so it selects all videos that are not on a playlist. Videos are entered on a playlist in playlistvideos as either a single videoid or a range.

 

So how can I say WHERE videos.ID != playlistvideos.videoid AND videos.ID NOT BETWEEN playlistvideos.range AND playlistvideos.range2

 

SELECT playlistvideos.videoid
     , playlistvideos.range
     , playlistvideos.range2
 , videos.ID
 , videos.VTitle
 , videos.IFilename
 , videos.videoid
 , videos.GTitle
FROM playlistvideos
INNER
  JOIN videos
    ON videos.ID = playlistvideos.videoid
   OR videos.ID BETWEEN playlistvideos.range AND playlistvideos.range2
WHERE videos.GTitle = '$game' AND videos.VTitle NOT LIKE '%</a>%' ORDER BY videos.ID DESC LIMIT $offset, $rowsPerPage

Link to comment
Share on other sites

The range part is m00t because you're joining ON NULL essentially, so for each video which doesn't have a videoplaylist associated with it (i.e. LEFT JOIN appears to be NULL) you can't test a NULL range....

 

Can you supply a sample layout + data so that I can test?

 

note: change the "= NULL" to "IS NULL"

Link to comment
Share on other sites

Quick update. I created my own tables to "mimic" what you have:

CREATE TABLE videos (
id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(50)
) ENGINE=InnoDB;

CREATE TABLE playlist (
id INT UNSIGNED NOT NULL,
video_id INT UNSIGNED NOT NULL,
UNIQUE INDEX (id,video_id)
);

INSERT INTO videos(name) VALUES('terminator'),('robocop'),('star wars I'),
('star wars II'),('star wars III'),('terminator 2'),('pretty woman');

INSERT INTO playlist(id,video_id)
VALUES(1,1),(1,2),(1,6),(2,2),(2,3);

 

SELECT * FROM videos v
LEFT JOIN playlist p ON p.video_id = v.id
WHERE p.video_id IS NULL;

 

And it would appear it needs to be "IS NULL" not "= NULL". The latter will cause a MySQL error. The first will give you your set.

i.e. all videos that are NOT in a playlist.

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.