按列对对SELECT结果进行排序

时间:2013-05-01 18:41:02

标签: postgresql postgresql-8.4

在以下PostgreSQL 8.4.13表中

author位用户向id位用户提供成绩):

# \d pref_rep;
                                       Table "public.pref_rep"
  Column   |            Type             |                         Modifiers
-----------+-----------------------------+-----------------------------------------------------------
 id        | character varying(32)       | not null
 author    | character varying(32)       | not null
 good      | boolean                     |
 fair      | boolean                     |
 nice      | boolean                     |
 about     | character varying(256)      |
 stamp     | timestamp without time zone | default now()
 author_ip | inet                        |
 rep_id    | integer                     | not null default nextval('pref_rep_rep_id_seq'::regclass)
Indexes:
    "pref_rep_pkey" PRIMARY KEY, btree (id, author)
Check constraints:
    "pref_rep_check" CHECK (id::text <> author::text)
Foreign-key constraints:
    "pref_rep_author_fkey" FOREIGN KEY (author) REFERENCES pref_users(id) ON DELETE CASCADE
    "pref_rep_id_fkey" FOREIGN KEY (id) REFERENCES pref_users(id) ON DELETE CASCADE

如何找到具有相同id和相同author_ip的伪造条目?

即。一些用户注册了多个帐户,然后为其他用户提交了错误的笔记(上面的goodfairnice列)。但我仍然可以通过author_ip地址识别它们。

我试图通过提取找到它们:

# select id, author_ip from pref_rep group by id, author_ip;
           id            |    author_ip
-------------------------+-----------------
 OK490496816466          | 94.230.231.106
 OK360565502458          | 78.106.102.16
 DE25213                 | 178.216.72.185
 OK331482634936          | 95.158.209.5
 VK25785834              | 77.109.20.182
 OK206383671767          | 80.179.90.103
 OK505822972559          | 46.158.46.126
 OK237791033602          | 178.76.216.77
 VK90402803              | 109.68.173.37
 MR16281819401420759860  | 109.252.139.198
 MR5586967138985630915   | 2.93.14.248
 OK341086615664          | 93.77.75.142
 OK446200841566          | 95.59.127.194

但我需要对上述结果进行排序。

如何根据对的数量(id,author_ip)desc进行排序?

1 个答案:

答案 0 :(得分:1)

select id, pr.author_ip
from
    pref_rep pr
    inner join
    (
        select author_ip
        from pref_rep
        group by author_ip
        having count(*) > 1
    ) s using(author_ip)
order by 2, 1