MySQL - 错误代码1054 - where子句中的未知列al.article_id

时间:2018-03-28 06:40:49

标签: mysql sql

我正在尝试执行以下查询,但无法找到问题。它继续抱怨

  

where子句

中的未知列al.article_id
SELECT al.article_id, COUNT(1) 
FROM article_likes al
WHERE al.created_at > '2018-03-25 11:01:00' 
AND al.article_id 
    NOT IN (
        SELECT article_id 
        FROM article_views 
        WHERE user_id = 48 
    )
AND (
    NOT EXISTS (
        SELECT 1
        FROM article_ignored AS ai 
        WHERE ai.user_id = 48 
        AND ai.article_id = al.article_id
    )
    OR 
    (
        EXISTS 
        (
        SELECT 1 
        FROM article_ignored aii 
        WHERE aii.user_id = 48 
        AND aii.article_id = al.article_id
        AND created_at > '2018-03-26 11:01:00'
        )
        AND
        EXISTS
        (
            SELECT (
                IF(al.post_type = 'article',
                (
                    SELECT DISTINCT (a.tag_id) 
                    FROM 
                    (SELECT tag_id FROM article_tags WHERE article_id = al.article_id) a
                    JOIN 
                    (SELECT tag_id FROM user_tags WHERE user_id = 48 AND type = 'interest') b
                    ON a.tag_id = b.tag_id
                ),
                (
                    SELECT DISTINCT (a.tag_id) 
                    FROM 
                    (
                    SELECT tag_id FROM article_tags 
                    WHERE article_id = (SELECT id FROM articles WHERE post_type = 'question' AND aid = al.article_id)
                    ) a
                    JOIN 
                    (SELECT tag_id FROM user_tags WHERE user_id = 48 AND type = 'interest') b
                    ON a.tag_id = b.tag_id
                    )
                )
            )
        )
    )
)
GROUP BY article_id 
ORDER BY COUNT(1) DESC
LIMIT 3

错误在以下部分,但我无法解决,有人可以帮忙。这是因为它嵌套在exists中 - >选择 - >如果

EXISTS
        (
            SELECT (
                IF(al.post_type = 'article',
                (
                    SELECT DISTINCT (a.tag_id) 
                    FROM 
                    (SELECT tag_id FROM article_tags WHERE article_id = al.article_id) a
                    JOIN 
                    (SELECT tag_id FROM user_tags WHERE user_id = 48 AND type = 'interest') b
                    ON a.tag_id = b.tag_id
                ),

1 个答案:

答案 0 :(得分:-1)

我想您可以尝试在此处指定特定的列“table.column”:

SELECT tag_id FROM article_tags WHERE article_id = al.article_id)

为:

SELECT tag_id FROM article_tags WHERE table.article_id = al.article_id)