ShadowStorm
ShadowStorm

Reputation: 851

SQL - Ambiguous Column

I can't see what is wrong with this query. I get an error saying:

"column article_id in from clause is ambiguous"

I understand that it may have something to do with table name aliases but not sure of how to fix. If the query was smaller I may be able to work something out but it's pretty confusing to me and every time I change something to try and fix it, something else stops - so I thought I'd ask first.

SELECT bt.topic_title, f.article_id, p.photo_id, ba.title, ba.slug,
IFNULL(c.cnt,0) comments, IFNULL(ph.cnt,0) photos, IFNULL(v.cnt,0) videos
FROM blog_article_followers AS f
LEFT OUTER JOIN (
    SELECT article_id, COUNT(comment_id) as cnt
    FROM blog_comments
    GROUP BY article_id) c
    ON f.article_id = c.article_id
LEFT OUTER JOIN (" _
    SELECT article_id, COUNT(photo_id) as cnt
    FROM photos
    GROUP BY article_id) ph
    ON f.article_id = ph.article_id
LEFT OUTER JOIN (
    SELECT article_id, COUNT(video_id) as cnt
    FROM videos
    GROUP BY article_id) v
    ON f.article_id = v.article_id
LEFT JOIN blog_topics bt ON f.topic_id = bt.topic_id
LEFT JOIN blog_articles AS ba USING (article_id)
LEFT JOIN photos AS p USING (article_id)
WHERE f.member_id = 100 AND p.cover = 1
ORDER BY f.follow_date DESC;

Upvotes: 0

Views: 671

Answers (2)

Mark Stosberg
Mark Stosberg

Reputation: 13411

Try replacing this:

LEFT JOIN blog_articles AS ba USING (article_id)
LEFT JOIN photos AS p USING (article_id)

With this

LEFT JOIN blog_articles AS ba ON f.article_id = ba.article_id
LEFT JOIN photos AS p ON f.article_id = photos.article_id

Upvotes: 1

Ibu
Ibu

Reputation: 43850

you have to rename the column

LEFT JOIN photos AS p USING (p.article_id)

or to whichever table article_id belongs to

Upvotes: 0

Related Questions