我有声明:
$statement_q = "
SELECT img
, title
, time
, videos.id
, date
FROM videos
JOIN videocategories
ON videos.id = videocategories.video_id
JOIN categories
ON categories.id = videocategories.category_id
WHERE categories.name != 'Western'
GROUP
BY videos.id
ORDER
BY videos.id
LIMIT {$startpoint} , {$per_page}";
层次:
如果电影有多个类别,它会显示它!
电影1:Werstern,Adventure(它会显示)
电影2:西方(一类,所以不会显示)
如何排除西方电影?
答案 0 :(得分:0)
这样的事情应该有效。我现在无法创建表格来验证它。
$statement_q = "SELECT img,title,time,videos.id as id,date FROM videos
INNER JOIN videocategories ON videos.id = videocategories.video_id
INNER JOIN categories on categories.id = videocategories.category_id
WHERE categories.name != 'Western'
AND not exists (select v.id from videocategories v where v.video_id = videos.id
and v.category_id <> videocategories.category_id)
GROUP by videos.id
ORDER BY videos.id
LIMIT {$startpoint} , {$per_page}";
答案 1 :(得分:0)
DROP TABLE IF EXISTS video_category;
CREATE TABLE video_category
(video_id INT NOT NULL
,category_id INT NOT NULL
,PRIMARY KEY(video_id,category_id)
);
INSERT INTO video_category VALUES
(101,1),(101,2),(102,1),(102,3);
SELECT * FROM video_category;
+----------+-------------+
| video_id | category_id |
+----------+-------------+
| 101 | 1 |
| 101 | 2 |
| 102 | 1 |
| 102 | 3 |
+----------+-------------+
SELECT *
FROM video_category x
LEFT
JOIN video_category y
ON y.video_id = x.video_id
AND y.category_id = 2;
+----------+-------------+----------+-------------+
| video_id | category_id | video_id | category_id |
+----------+-------------+----------+-------------+
| 101 | 1 | 101 | 2 |
| 101 | 2 | 101 | 2 |
| 102 | 1 | NULL | NULL |
| 102 | 3 | NULL | NULL |
+----------+-------------+----------+-------------+
SELECT DISTINCT x.video_id
FROM video_category x
LEFT
JOIN video_category y
ON y.video_id = x.video_id
AND y.category_id = 2
WHERE y.category_id IS NULL;
+----------+
| video_id |
+----------+
| 102 |
+----------+