Column doesn't exist?

前端 未结 1 597
醉酒成梦
醉酒成梦 2020-12-11 06:32

Was wondering if someone could help me out a little with this query:

SELECT u1.id,count(DISTINCT u2.userstatus) as TEMPCOLUMN FROM users AS u1
JOIN friendssy         


        
相关标签:
1条回答
  • 2020-12-11 07:07

    You can't reference a column alias in the WHERE clause.

      SELECT u1.id,
             COUNT(DISTINCT u2.userstatus) as TEMPCOLUMN 
        FROM USERS AS u1
        JOIN friendssym ON u1.id = friendssym.user_id
        JOIN USERS as u2 ON friendssym.friend_id = u2.id      
    GROUP BY u1.id
      HAVING COUNT(DISTINCT u2.userstatus) = 1
    

    In traditional SQL, the earliest you can reference a column alias is the ORDER BY clause. But MySQL and SQL Server allow access in the HAVING and GROUP BY clauses.

    0 讨论(0)
提交回复
热议问题