0

我在做查询时有一点错误。

它说:Unknown column 'friend_id' in 'on clause'

SELECT DISTINCT
       CASE friends.user_id_1 
            WHEN 1 THEN friends.user_id_2 
            ELSE friends.user_id_1 
       END AS friend_id,
       friends.created
       /*users.name_surname,
       users.avatar*/
FROM friends
JOIN users ON friend_id = users.id
WHERE 1 IN (friends.user_id_1, friends.user_id_2) /*AND friends.status = 1*/

问题显然是friend_id,因为它不是一个真正的专栏。如何修复错误?

4

1 回答 1

0

加入后SELECT发生,因此不能使用别名。

试试这个:

SELECT DISTINCT
   friends.friend_id,
   friends.created
   /*users.name_surname,
   users.avatar*/
FROM (
    SELECT created,
        CASE user_id_1 WHEN 1 THEN user_id_2 
            ELSE user_id_1 
        END AS friend_id
    FROM friends
    WHERE 1 IN (user_id_1, user_id_2) /*AND status = 1*/
) friends
JOIN users ON friends.friend_id = users.id
于 2013-10-30T19:23:51.957 回答