0

我有一个 MySQL 表,其中包含以下数据。

surveyid | staff  | username | ipaddress | rating  | comments 
---------|--------|----------|-----------|---------|----------
0        | staff1 | user1    | 1.1.1.1   | 10      | none
1        | staff2 | user2    | 1.2.1.1   | 5       | none
2        | staff2 | user3    | 1.2.1.1   | 10      | none
3        | staff2 | user2    | 1.2.1.1   | 6       | none
4        | staff3 | user4    | 1.1.1.51  | 10      | none
5        | staff4 | user3    | 1.21.12.1 | 9       | none
6        | staff5 | user2    | 1.12.1.1  | 10      | none

我想要一个查询,仅当同一员工的多个用户使用一个 ipaddress 时才会选择surveyid、staff、username 和 ipaddress。

基本上我想找出哪些员工有多个用户提交了同一个 IP 地址。

我尝试了以下方法,但它不起作用。

SELECT * FROM `comment_data` CD 
INNER JOIN
(SELECT `ipaddress`, COUNT(DISTINCT `staff`)
FROM `comment_data` GROUP BY `ipaddress` HAVING COUNT(DISTINCT `staff`) > 1 )
USECOUNT ON USECOUNT.`ipaddress` = CD.`ipaddress`

我还尝试了以下查询。

SELECT * FROM `commend_data` WHERE `staff`+`ipaddress` IN
(SELECT `staff`+`ipaddress` FROM `comment_data` GROUP BY `staff`, `ipaddress`
HAVING COUNT(`surveyid`) > 1) 

此外,我想确保显示所有重复项,并且我不想只显示计数。

如果您需要更多信息,请询问。

谢谢保罗

4

2 回答 2

4
select surveyid, staff, username, ipaddress
from table1
where ipaddress+staff in 
(
select ipaddress+staff
from table1
group by ipaddress,staff
having count(surveyid)>1
)

SQL 小提琴

于 2013-07-11T14:01:27.177 回答
0
SELECT surveyid,
        PT.staff,
        username,
        PT.ipaddress
   FROM PaulTable PT
        INNER JOIN 
        (
           SELECT ipaddress, staff
             FROM PaulTable
                  GROUP BY ipaddress, staff
                  HAVING COUNT(DISTINCT username) > 1
        ) USECOUNT
            ON USECOUNT.ipaddress = PT.ipaddress
            AND USECOUNT.staff = PT.staff
于 2013-07-11T14:06:12.167 回答