简体   繁体   中英

MySQL COUNT with INNER JOIN

The following query returns me the total of 2 rows instead of 1 because it happens the video to have two categories associated.

SELECT COUNT(video.id) AS `total`
FROM `videos` AS `video`
INNER JOIN `videos_categories` AS `video_category` ON `video_category`.`video_id` = `video`.`id`
INNER JOIN `categories` AS `category` ON `category`.`id` = `video_category`.`category_id`
WHERE video.title = 'John Mcane' OR category.description = 'Traditional'

The table videos_categories has two lines and that's the problem.

id | video_id | category_id
1    1          1
1    1          2

I can make a count to my array in PHP but that's not correct. Any ideas of how can I solve this? Group by didn't work.

COUNT(X)计算所遇到的X值的总数,这些值不为null,COUNT(DISTINCT X)应该为您提供所需的值(假设X是唯一字段)。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM