Mysql Query无法按预期工作

时间:2011-09-29 10:42:42

标签: mysql

我遇到MySql查询问题:

我有两张桌子:

Ips的:

ID  
timestamp   
user_id     
ip  
dns 

黑名单用户:

timestamp   
user_id     
mod_id moderator

我想要做的是获取与特定用户相关的ips黑名单用户数。我现在的询问似乎是这样的:

SELECT count( bl.user_id ) AS c, ips_user.ip
FROM user_ips AS ips
INNER JOIN user_ips AS ips_user ON ips.ip = ips_user.ip
INNER JOIN user_blacklist AS bl ON ips_user.user_id = bl.user_id
WHERE ips.user_id = 239925
GROUP BY bl.user_id

但是此查询会导致与用户ID黑名单关联的数字ip,并且不会搜索具有相同IP的其他用户。

提前感谢您的答案。

1 个答案:

答案 0 :(得分:0)

您需要将group by子句与所选字段相匹配 通常,您可以在group by子句中的select部分中复制所有非聚合字段。

这是你想要的吗?

SELECT 
  count( bl.user_id ) AS c
  , ips_user.ip 
FROM user_ips AS ips 
INNER JOIN user_ips AS ips_user ON ips.ip = ips_user.ip 
INNER JOIN user_blacklist AS bl ON ips_user.user_id = bl.user_id 
WHERE ips.user_id = 239925 
GROUP BY ips.user_ip  <<-- should match field in the select clause