2013-05-01 82 views
0

在下面的PostgreSQL 8.4.13表排序SELECT結果通過對列

(其中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 

如何發現僞造的條目,其中有相同的idauthor_ip一樣嗎?

I.e.一些用戶註冊了多個帳戶,然後爲其他用戶提交壞筆記(上面的good,fair,nice列)。但我仍然可以通過他們的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

'ORDER BY COUNT(*)DESC'? – 2013-05-01 18:44:10

+0

嗯,是的,它確實有效 - 但不會產生我所希望的... – 2013-05-01 18:50:33

+1

然後,我認爲你需要澄清你希望得到的東西:) – 2013-05-01 18:51:43

回答

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