Received: from localhost (maia-2.hub.org [200.46.204.187]) by postgresql.org (Postfix) with ESMTP id 79EC79F9FE1 for ; Thu, 14 Dec 2006 09:11:03 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.187]) (amavisd-new, port 10024) with ESMTP id 11114-01 for ; Thu, 14 Dec 2006 09:10:51 -0400 (AST) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.4 Received: from ug-out-1314.google.com (ug-out-1314.google.com [66.249.92.172]) by postgresql.org (Postfix) with ESMTP id 834F99F9F7B for ; Thu, 14 Dec 2006 09:10:56 -0400 (AST) Received: by ug-out-1314.google.com with SMTP id k3so486925ugf for ; Thu, 14 Dec 2006 05:10:54 -0800 (PST) DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=beta; d=gmail.com; h=received:message-id:date:from:to:subject:cc:in-reply-to:mime-version:content-type:references; b=dTtCuJYpe5eIsiha4sLH+5csedwcJw/IchNKmQmz5uN6OA/lU00r2l9qEKmra0lLStCUm31pjFNqBgz7H8Gwjh6G8xRtMrkoGfORxR1eHjzbtJMrCGW4Mxfxj2Bomex8qU2d8WeM3Nok0JiCceODut5PEbdDnC/pgETVisW8PbU= Received: by 10.78.164.13 with SMTP id m13mr673178hue.1166101853969; Thu, 14 Dec 2006 05:10:53 -0800 (PST) Received: by 10.78.150.17 with HTTP; Thu, 14 Dec 2006 05:10:53 -0800 (PST) Message-ID: <682126990612140510j61d2268fq80235b02a550eccd@mail.gmail.com> Date: Thu, 14 Dec 2006 18:40:53 +0530 From: "Ashish Ahlawat" To: Ragnar Subject: Re: join and sort on 'best match' Cc: "Dirk Griffioen" , pgsql-sql@postgresql.org In-Reply-To: <1166005787.6369.79.camel@localhost.localdomain> MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_Part_132671_8498241.1166101853931" References: <457FC751.6080104@code-shop.com> <1166005787.6369.79.camel@localhost.localdomain> X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200612/107 X-Sequence-Number: 27063 ------=_Part_132671_8498241.1166101853931 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: quoted-printable Content-Disposition: inline hi pls tell me ---- if table *Item 3 : news, nature, greenpeace, whale has all clmn y v need join ??* *Ashish* On 12/13/06, Ragnar wrote: > > On mi=F0, 2006-12-13 at 10:26 +0100, Dirk Griffioen wrote: > > > I have been breaking my head on the following problem: how to join 2 > > tables and sort the results on the best match. > ^^^^^^^^^^^^^ > > - there are 3 tables, items, tags and items_tags. The items_tags table > > links items to tags. > > - I have one item which has certain tags, and I want to look up all > > the other items that have those tags as well > > looks to me like you want to join: > items->item_tags->tags->item_tags->items > > so the basic select is: > SELECT * > FROM items AS i1 > JOIN items_tags AS it1 ON (it1.item_id =3D i1.id) > JOIN tags AS t ON (t.tag_id =3D it1.tag_id) > JOIN items_tags AS it2 ON (it2.tag_id =3D t.tag_id) > JOIN items AS i2 ON (i2.id =3D it2.item_id) > WHERE i1.id=3D? > > > - results should be sorted and presented by 'best match': first all > > the items that have 3 tags in common, then 2 and last 1 > > this would be: > > SELECT i1.id,i2.id,COUNT(*) as quantity > FROM items AS i1 > JOIN items_tags AS it1 ON (it1.item_id =3D i1.id) > JOIN tags AS t ON (t.tag_id =3D it1.tag_id) > JOIN items_tags AS it2 ON (it2.tag_id =3D t.tag_id) > JOIN items AS i2 ON (i2.id =3D it2.item_id) > WHERE i1.id=3D? > GROUP by i1.id,i2.id > ORDER BY quantity DESC > > > I thought I had found the solution (my test cases worked), but I now > > find cases that should be found by the query but are not. > > if this does not work, please provide us with a counter example. > > gnari > > > > ---------------------------(end of broadcast)--------------------------- > TIP 4: Have you searched our list archives? > > http://archives.postgresql.org > ------=_Part_132671_8498241.1166101853931 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline
hi pls tell me ----
 
if table Item 3 := news, nature, greenpeace, whale has all clmn y v n= eed join ??

Ashish



 
On 12/13/06, Ragnar <gnari@hive.is> wrot= e:
On mi=F0, 2006-12-13 at 10:26 +0= 100, Dirk Griffioen wrote:

> I have been breaking my head on the = following problem: how to join 2
> tables and sort the results on the best match.
^^^^^^^^^^^^^> - there are 3 tables, items, tags and items_tags. The items_tags tabl= e
> links items to tags.
> - I have one item which has certain = tags, and I want to look up all
> the other items that have those tags as well

looks to me li= ke you want to join:
items->item_tags->tags->item_tags->item= s

so the basic select is:
SELECT *
FROM    = ;  items AS i1
      JOIN items_tags AS it1 ON (it1.item_id = =3D i1.id)
     &n= bsp;JOIN tags AS t ON (t.tag_id =3D it1.tag_id)
    =   JOIN items_tags AS it2 ON (it2.tag_id =3D t.tag_id)
 &n= bsp;    JOIN items AS i2 ON ( i2.id =3D it2.item_id)
WHERE i1.id=3D?
<= br>> - results should be sorted and presented by 'best match': first all=
> the items that have 3 tags in common, then 2 and last 1

thi= s would be:

SELECT i1.id,= i2.id,COUNT(*) as quantity
FROM      i= tems AS i1
      JOIN items_tags AS it1 ON= (it1.item_id =3D i1.id)
   =    JOIN tags AS t ON ( t.tag_id =3D it1.tag_id)
      JOIN items_= tags AS it2 ON (it2.tag_id =3D t.tag_id)
     &= nbsp;JOIN items AS i2 ON (i2.id =3D it2.item_i= d)
WHERE i1.id=3D?
GROUP by i1.id ,i2.id
ORDER BY quantity DESC

> I= thought I had found the solution (my test cases worked), but I now
>= find cases that should be found by the query but are not.

if this d= oes not work, please provide us with a counter example.

gnari



---------------------------(end of broadcast)-= --------------------------
TIP 4: Have you searched our list archives?
           &nb= sp;  http://archives.p= ostgresql.org

------=_Part_132671_8498241.1166101853931--