2017-05-17 74 views
0

我有一個簡單的查詢,但實際上並不知道。我需要的結果從一個子查詢中where條款:mysql從其他表中計數行並在其中使用它

SELECT u.*,(SELECT COUNT(ur_id) FROM #__user_relation WHERE (uid1=u.u_id AND 
uid2='#me') or (uid2=u.u_id AND uid1='#me')) as count FROM #__user as u WHERE 
u.u_id!='me' AND count=0 

Unknown column 'count' in 'where clause' 

回答

0
SELECT u.*,(
SELECT COUNT(ur_id) 
FROM #__user_relation 
WHERE (uid1=u.u_id AND uid2='#me') or (uid2=u.u_id AND uid1='#me')) as count 
FROM #__user as u 
WHERE u.u_id!='me' **having count=0** 
+0

那麼簡單......非常感謝你! +1 – Ben