Mysql 查询不按预期工作

标签 mysql

我在使用 MySql 查询时遇到问题:

我有 2 个表:

提示:

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的用户。

预先感谢您的回答。

最佳答案

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

这是你想要的吗?

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

关于Mysql 查询不按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7596123/

相关文章:

mysql - 如何更改当前时间?

php - json 列与多列

mysql - 表存在于显示表中,但当我尝试从中选择时不存在

PHP/PDO : Prepared statements don't work when creating a table?

MySQL:字段中的空白字符实际上不是空白。它是什么?

android - 如何通过 android Volley 或 JSON 从 mysql 检索特定行数据?

mysql - 将数据保存在多个文本行与 blob 上,哪个效果更好?

php - 在Codeigniter中: How to get url if i clicked on the fetched data from database

mysql - 计算列 MySQL 中的唯一数字

php - MySQL 语法错误 - 要查询的数组