无法解决MySQL Query问题

时间:2013-07-11 13:58:55

标签: mysql

我有一个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) 

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

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

由于 保罗

2 个答案:

答案 0 :(得分: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 FIDDLE

答案 1 :(得分: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