0

如何进行查询以仅获取具有相同screen_nameskip= 0 值的记录?

---------------------------
| id | screen_name | skip |
---------------------------
| 1  | mary        | 0    |
| 2  | john        | 0    |
| 3  | tom         | 1    |
| 4  | mary        | 0    |
| 5  | ben         | 1    |
| 6  | john        | 1    |
---------------------------
4

2 回答 2

2
SELECT  screen_Name
FROM    tableName
WHERE   skip = 0
GROUP   BY screen_name
HAVING  COUNT(*) > 1

更新

如果您想获取所有记录而不仅仅是screen_name, 使用JOIN而不是IN

SELECT  a.*
FROM    tableName a
        INNER JOIN
        (
            SELECT  screen_Name
            FROm    tableName
            WHERE   skip = 0
            GROUP   BY screen_name
            HAVING  COUNT(*) > 1
        ) b ON a.screen_name = b.screen_name

要获得更快的性能,请添加一个INDEXon 列screen_name以获得更快的性能。

ALTER TABLE tableName ADD INDEX index_name (screen_Name)
于 2013-01-25T07:37:15.240 回答
0

你可以尝试使用:

select id,screen_name,skip
  from TABLE
 where skip=0
   and screen_name in (select t2.screen_name
                         from TABLE t2
                     group by t2.screen_name
                    having count(*)>1 
                      )

这将为您提供所有带有 skip=0 和重复 screen_name 的记录

于 2013-01-25T07:39:57.853 回答