1

问题是针对示例表结构:

我的表

fielda, fieldb
val1    valx
val2    valy
val3    valz
val1    vala

我想通过查询从 mytable 中进行选择:

fielda, fieldb
val1    valx
val1    vala

基本上从重复 fielda 的表中选择 *。

然后使用不同的查询从 mytable 中选择

fielda, fieldb
val2    valy
val3    valz

fielda 上的 val1 被视为重复项,因此它没有被选中,其余的都被选中。

4

3 回答 3

3

此查询fieldA对列表顶部重复的表进行排序。

SELECT  a.*
FROM    tableName a
        INNER JOIN
        (
            SELECT fieldA, COUNT(*) totalCount
            FROM tableName
            GROUP BY fieldA
        ) b ON a.fieldA = b.fieldA
ORDER BY b.totalCount DESC

此查询仅选择重复记录。

SELECT  a.*
FROM    tableName a
        INNER JOIN
        (
            SELECT fieldA, COUNT(*) totalCount
            FROM tableName
            GROUP BY fieldA
        ) b ON a.fieldA = b.fieldA
WHERE b.totalCount > 1
ORDER BY b.totalCount DESC

而这一个,选择不在第二个查询中的记录(上面

SELECT  a.*
FROM    tableName a
        INNER JOIN
        (
            SELECT fieldA, COUNT(*) totalCount
            FROM tableName
            GROUP BY fieldA
        ) b ON a.fieldA = b.fieldA
WHERE b.totalCount = 1
ORDER BY b.totalCount DESC
于 2012-10-27T15:32:28.563 回答
2
select fielda, fieldb from mytable group by fielda having count(*) >1

select fielda, fieldb from mytable group by fielda having count(*) =1
于 2012-10-27T15:31:34.037 回答
0

为了第一:

SELECT * FROM tbl GROUP BY fielda HAVING COUNT(*)>1

对于第二个:

从 tbl 中选择 DISTINCT(fielda)

于 2012-10-27T15:32:04.383 回答