From: | "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com> |
---|---|
To: | Pól Ua Laoínecháin <linehanp(at)tcd(dot)ie> |
Cc: | pgsql-novice <pgsql-novice(at)postgresql(dot)org> |
Subject: | Re: Want records to be UNIQUE. When searching for dupes, stop on first matching record. |
Date: | 2019-04-18 21:40:53 |
Message-ID: | CAKFQuwaLUp_KiNPPCTz+Ax=urmc5u-yAC_jr6j8Tn1Mp3=+EsQ@mail.gmail.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-novice |
On Thu, Apr 18, 2019 at 1:15 PM Pól Ua Laoínecháin <linehanp(at)tcd(dot)ie> wrote:
> SELECT (user_id, article_id)::text, count(*)
> FROM test_article
> WHERE 1 = (SELECT 1)
> GROUP BY user_id, article_id
> HAVING count(*) > 1
>
> But what I really want (what I really, really want - apologies to the
> Spice Girls) to know is, is there a query which will run and stop on
> the first match? Using ANY, ALL or EXISTS or some construct like that?
>
>
Create a unique index over user_id, article_id on the test_article table.
The system will never allow a duplicate to be inserted in the first place.
That said your query is overly complicated...
SELECT user_id, article_id
FROM test_article
GROUP BY user_id, article_id
HAVING count(*) > 1
A where clause is optional and the composite and count in the select list
aren't really needed unless you have some other unstated requirement for
them.
David J.
From | Date | Subject | |
---|---|---|---|
Next Message | Pól Ua Laoínecháin | 2019-04-18 22:26:00 | Re: Want records to be UNIQUE. When searching for dupes, stop on first matching record. |
Previous Message | Pól Ua Laoínecháin | 2019-04-18 20:15:23 | Want records to be UNIQUE. When searching for dupes, stop on first matching record. |