错误代码:1054' where子句'中的未知列

时间:2015-01-28 10:48:11

标签: mysql sql

我想获取每位艺术家所有内容的总观看次数

wp_content_details.artist_id = wp_artist.id在我使用静态值Unknown column in where clause时生成错误“wp_content_details.artist_id = 29”然后确定但我需要使用条件wp_content_details.artist_id = wp_artist.id

SELECT wp_artist.id
    ,wp_artist.NAME
    ,(
        SELECT SUM(total_user) AS total_view
        FROM (
            SELECT COUNT(DISTINCT (user_id)) AS total_user
            FROM wp_views
            LEFT JOIN wp_content_details ON wp_content_details.content_id = wp_views.content_id
            WHERE wp_content_details.artist_id = wp_artist.id /* Its ok if use:  wp_content_details.artist_id = 29*/
                AND wp_content_details.role_id = 2
            GROUP BY wp_views.content_id
            ) content_views
        ) AS total_views
FROM wp_artist
WHERE wp_artist.id IN (
        SELECT DISTINCT (wp_content_details.artist_id)
        FROM wp_content_details
        WHERE wp_content_details.artist_id = wp_artist.id
            AND wp_content_details.role_id = 2
        )
ORDER BY total_views DESC
    ,wp_artist.NAME ASC

请帮帮我。如果您有任何疑惑,请问我。

1 个答案:

答案 0 :(得分:1)

wp_artist.ID超出范围。尝试

    SELECT SUM(total_user) AS total_view
    FROM (
        SELECT COUNT(DISTINCT (user_id)) AS total_user, wp_content_details.artist_id
        FROM wp_views
        LEFT JOIN wp_content_details ON wp_content_details.content_id = wp_views.content_id
        WHERE wp_content_details.role_id = 2
        GROUP BY wp_views.content_id, wp_content_details.artist_id 
        ) content_views
Where content_views.artist_id = wp_artist.id
    ) AS total_views