按计数排序的多个连接忽略条件的位置

时间:2015-05-29 08:34:54

标签: mysql count where

我尝试仅从category_video_rel获取属于特定类别的video_index中的行,并按结果排序COUNT次。这是我正在使用的查询:

  SELECT
        COUNT(DISTINCT view_count.id) AS count,
        view_count.remove,
        view_count.video_id,
        video_index.id AS video_id,
        video_index.active,
        video_index.remove,
        video_index.title AS title,
        video_index.date_published AS date_published,
        category_video_rel.active,
        category_video_rel.remove,
        category_video_rel.video_id AS cv_video_id,
        category_video_rel.category_id AS category_id
    FROM 
        view_count JOIN video_index 
            ON view_count.video_id = video_index.id,
        category_video_rel JOIN video_index AS v
            ON category_video_rel.video_id = v.id
    WHERE
        view_count.remove = '0' AND
        video_index.active = '1' AND
        video_index.remove = '0' AND
        video_index.date_published <= '$current_time' AND
        category_video_rel.category_id = '$category_id' AND
        category_video_rel.active = '1' AND
        category_video_rel.remove = '0'
    GROUP BY
        video_index.id
    ORDER BY
        count DESC

问题是无论类别如何,它都会输出video_index中视图计数大于0的所有行。基本上,它忽略了WHERE条件中的“category_video_rel.category_id ='$ category_id'”。

我不知道我做错了什么,请帮忙。

1 个答案:

答案 0 :(得分:1)

您的FROM子句混合旧样式连接和新样式连接 而是尝试:

FROM 
    view_count JOIN video_index 
        ON view_count.video_id = video_index.id
    JOIN category_video_rel 
        ON category_video_rel.video_id = video_index.id