简体   繁体   中英

Error Code: 1052. Column 'ATTRIBUTE' in field list is ambiguous

When I attempt to run the bellow section, I get the following error message. I am still new to SQL so I am guessing that the issue is relatively simple, making you experts cringe just a little bit. Regardless, thank you so so much to anyone who replies.

SELECT DRINKER, COUNT(RATING)
FROM DRINKERS LEFT JOIN LIKES
ON LIKES.DRINKER = DRINKERS.DRINKER
WHERE LIKES.RATING < 0
GROUP BY LIKES.DRINK
ORDER BY DRINK ASC;

I get the error:

19:17:07 SELECT DRINKER, COUNT(RATING) FROM DRINKERS LEFT JOIN LIKES ON LIKES.DRINKER = DRINKERS.DRINKER WHERE LIKES.RATING < 0 GROUP BY LIKES.DRINK ORDER BY DRINK ASC LIMIT 0, 1000

Error Code: 1052. Column 'DRINKER' in field list is ambiguous 0.00031 sec

Thanks for any help at all!

You should qualify all column references. Because you are using LEFT JOIN , I am guessing that you want all drinkers returned. Your WHERE clause limits the result set to only those drinkers with negative ratings.

If you want all drinkers, then that condition needs to move to the ON clause. I also recommend table aliases:

SELECT D.DRINKER, COUNT(L.RATING)
FROM DRINKERS D LEFT JOIN
     LIKES L
     ON L.DRINKER = D.DRINKER AND
        L.RATING < 0
GROUP BY D.DRINKER
ORDER BY D.DRINKER ASC;

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