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
https://forums.phpfreaks.com/topic/91801-join-where-help/
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
https://forums.phpfreaks.com/topic/91801-join-where-help/#findComment-470604
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
https://forums.phpfreaks.com/topic/91801-join-where-help/#findComment-470612
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.