Re: limit with subselect

From: "A(dot) Kretschmer" <andreas(dot)kretschmer(at)schollglas(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: limit with subselect
Date: 2008-07-25 10:22:10
Message-ID: 20080725102210.GD24432@a-kretschmer.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

am Fri, dem 25.07.2008, um 12:02:23 +0200 mailte A B folgendes:
> Sorry if this is a double posting, I think the previous message was lost.
>
> I have two tables
> T (
> id int primary key,
> a int,
> b int
> )
>
> T2 (
> id int references T,
> c int
> );
>
> and I wish to get 20 lines from T like this
>
> select id,a,b from T where id not in (select id from T2 where c=5) limit 20;
>
> but that does not seem to work. How can I get what I want? What 20
> records are selected is not important. I just need 20.

Works for me:

test=# create table t (id serial primary key, a int);
NOTICE: CREATE TABLE will create implicit sequence "t_id_seq" for serial column "t.id"
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "t_pkey" for table "t"
CREATE TABLE
test=*# create table t2 (id int references t, c int);
CREATE TABLE
test=*# insert into t (a) select * from generate_Series(10,50) s;
INSERT 0 41
test=*# insert into t2 values (1,1);
INSERT 0 1
test=*# insert into t2 values (2,2);
INSERT 0 1
test=*# insert into t2 values (3,3);
INSERT 0 1
test=*# insert into t2 values (4,4);
INSERT 0 1
test=*# select * from t where id not in (select id from t2 where c=3) limit 5;
id | a
----+----
1 | 10
2 | 11
4 | 13
5 | 14
6 | 15
(5 rows)

Andreas
--
Andreas Kretschmer
Kontakt: Heynitz: 035242/47150, D1: 0160/7141639 (mehr: -> Header)
GnuPG-ID: 0x3FFF606C, privat 0x7F4584DA http://wwwkeys.de.pgp.net

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message A B 2008-07-25 10:31:56 Re: limit with subselect
Previous Message A B 2008-07-25 10:02:23 limit with subselect