简体   繁体   English

MySQL-选择多个具有相同值的行,其中一个必须不同

[英]MySQL - Select multiple rows with the same values with one having to be different

I have a table for IP addresses used and the associated userId. 我有一个使用的IP地址和相关的userId表。 Now I know how I would select multiple entries of the same IP but how would I go about only selecting multiple entries WITH a different userId? 现在,我知道如何选择相同IP的多个条目,但是如何只选择具有不同userId的多个条目呢?

You can use conditional aggregation over each ip address and check to see if more than one user be associated with that ip address. 您可以对每个IP地址使用条件聚合,并检查是否有多个用户与该IP地址关联。

SELECT ip
FROM yourTable
GROUP BY ip
HAVING COUNT(DISTINCT userid) > 1    -- or a higher number if you want

you can use sql join do it for you INNER JOIN gets all records from one table that have some related entry in a second table 您可以使用sql join为您做到这一点INNER JOIN从一个表中获取所有记录,而在第二个表中具有一些相关条目

LEFT JOIN gets all records from the LEFT linked table but if you have selected some columns from the RIGHT table, if there is no related records, these columns will contain NULL LEFT JOIN从LEFT链接表中获取所有记录,但是如果您从RIGHT表中选择了一些列,则如果没有相关记录,则这些列将包含NULL。

RIGHT JOIN is like the above but gets all records in the RIGHT table RIGHT JOIN与上面类似,但是获得了RIGHT表中的所有记录

FULL JOIN gets all records from both tables and puts NULL in the columns where related records do not exist in the opposite table FULL JOIN从两个表中获取所有记录,并将NULL放入相反表中不存在相关记录的列中

SQL joins SQL联接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM