开发者

Mysql Query does not work as expected

i'm having problems with a MySql query:

I have 2 tables as this:

I开发者_JAVA百科ps:

ID  
timestamp   
user_id     
ip  
dns 

Blacklisted users:

timestamp   
user_id     
mod_id moderator

What I want to do is to get the number of user blacklisted with ips related to an specific user. My query right now seems like that:

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

But this query results in the number ip associated with the user id blacklisted, and does not search for others users with the same ip.

Thanks in advance for the answers.


You need to match your group by clause with the fields selected.
Normally you'd copy all non-aggregated fields in the select part in the group by clause.

Is this what you want?

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜