X-Original-To: pgsql-sql-postgresql.org@postgresql.org Received: from localhost (wm.hub.org [200.46.204.128]) by postgresql.org (Postfix) with ESMTP id A81BE9FB388 for ; Tue, 5 Sep 2006 13:09:41 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.128]) (amavisd-new, port 10024) with ESMTP id 27113-09 for ; Tue, 5 Sep 2006 16:09:26 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey- Received: from floppy.pyrenet.fr (floppy.pyrenet.fr [194.116.145.2]) by postgresql.org (Postfix) with ESMTP id 0FDC09FB38C for ; Tue, 5 Sep 2006 13:05:26 -0300 (ADT) Received: by floppy.pyrenet.fr (Postfix, from userid 106) id 1C01F308EA; Tue, 5 Sep 2006 18:05:26 +0200 (MET DST) From: "codeWarrior" X-Newsgroups: pgsql.sql Subject: Re: Subquery alternatives? Date: Tue, 5 Sep 2006 09:07:36 -0700 Organization: iDynaTECH, Inc. Lines: 45 Message-ID: References: <1157116342.151133.121010@b28g2000cwb.googlegroups.com> Reply-To: "codeWarrior" X-Complaints-To: usenet@news.hub.org X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2900.2869 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2962 X-RFC2646: Format=Flowed; Original To: pgsql-sql@postgresql.org X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=1.162 tagged_above=0 required=5 tests=FORGED_HOTMAIL_RCVD2 X-Spam-Level: * X-Archive-Number: 200609/29 X-Sequence-Number: 26240 I dont think you need the double-left join.... SELECT * FROM STORIES ST LEFT JOIN TAGS TG ON TG.tagkey = ST.storykey WHERE TG.tag = "science" "MRKisThatKid" wrote in message news:1157116342.151133.121010@b28g2000cwb.googlegroups.com... > Hi, i've posted this in various places but I'm really struggling to > find an answer that works for me... > > Say I've got a table of stories and a table of tags which apply to the > stories. > > Say you've been using the tags science and unfinished, and you want all > the stories which are science based and are completed.. that is, all > the stories tagged with "science" but not "unfinished". > > I could do this using sub-queries quite easily, especially the exists / > not exists function in mysql. > > Unfortunately I'm implementing this kind of query into a web > application i'm developing and I want it to be usable on as many > different server set-ups as possible. > > select * from stories > left join tags as tags1 on (tags1.tagkey = storykey) > left join tags as tags2 on (tags2.tagkey = storykey) > where tags1.tag = "science" > and not tags2.tag = "unfinished" > > It would be wonderful if the above worked, but of course it doesn't > because when the db engine makes all the possible combinations it will > always find a match where tags2 doesn't contain "unfinished". > > Can anyone think of a way of achieving this without sub-queries? > > ... The best solution I've been offered so far is to use group_concat > and find_in_set, but these are mysql specific functions and are not > portable. Do any of you guys know who I should go about this? >