mysql从其他表中计算行并在其中使用它

时间:2017-05-17 08:10:41

标签: mysql count where

我有一个简单的查询但实际上不知道。我需要来自where子句中的子查询的结果:

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'

1 个答案:

答案 0 :(得分: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**