mysql - 如何按类别排除记录

标签 mysql

我有声明:

$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}";

层次结构:

enter image description here

如果电影有多个类别,它会显示它!

Movie 1 : Werstern, Adventure (它会放映)

电影 2:西部片(一类所以不会显示)

如何排除西方电影?

最佳答案

像这样的东西应该可以工作。我现在无法创建表来验证它。

$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}";

关于mysql - 如何按类别排除记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32081683/

相关文章:

MYSQL DELETE WHERE 条件 (COUNT = 0)

mysql - 如何在 NodeJS 的变量中存储 "count(*)"值?

php - 在 PHP 中使用 group_concat 回显 mysql 数据

MySQL Group By 并跳过空值分组

Mysql修剪将字段的开头修剪到指定的字符串

mysql - 在mysql查询中获取至少10个值

mysql - 将 MS-SQL 服务器文本类型插入 MySQL 服务器

MySQL : named parameters with PREPARE command?

Mysql查询除

mysql - 如何使用 Eloquent Laravel 嵌套两个查询