我在这里有这个 mySQL 查询,它应该给我一个用户的所有朋友的数量。但是,我需要进一步过滤它以仅包含由“status = 20”指定的已确认好友请求。当我尝试在 WHERE 子句中执行此操作时,我不断收到以下错误
错误代码:1054。“字段列表”中的“f.status”列未知。
我的查询在这里:
Select
u.uuid, u.nick,
count(distinct f.friend_uuid) as number_of_friends, f.status
From
user u
Left Outer Join (
Select user_uuid, friend_uuid from user_friend
Union All
Select friend_uuid, user_uuid from user_friend
) as f
On u.uuid = f.user_uuid
WHERE f.status = '20'
GROUP BY 1,2,4
HAVING number_of_friends >=10;
您的查询有 3 个问题。
您可以通过这种方式使用别名。创建结果集后将创建别名。结果集将使用 HAVING 子句。因此,have 子句必须在结果集中具有名称。这在以下查询中得到纠正。
您已经使用了 WHERE f.status = '20'。f 用作左外连接。如果您在 where 子句中使用 ouer join 列,那么它将成为内连接。这在下面的查询中得到纠正。
带有左外连接的内部查询在选择中没有状态列。因此它抛出错误。
选择u.uuid, u.nick, count(distinct f.friend_uuid) as number_of_friends, f.status From user u Left Outer Join ( Select user_uuid,friend_uuid, [STATUS_COLUMN REQUIRED] from user_friend Union All Selectfriend_uuid, user_uuid, [STATUS_COLUMN REQUIRED] ] from user_friend ) as f On u.uuid = f.user_uuid and f.status = '20' GROUP BY 1,2,4 HAVING count(distinct f.friend_uuid) >=10;
如果您仍然遇到任何问题,请告诉我。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句