Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id 886A1476F99 for ; Wed, 15 Jan 2003 14:16:36 -0500 (EST) Received: from smtp-server3.tampabay.rr.com (smtp-server3.tampabay.rr.com [65.32.1.41]) by postgresql.org (Postfix) with ESMTP id A61CA4771AC for ; Wed, 15 Jan 2003 13:30:01 -0500 (EST) Received: from mattspc ([65.34.52.169]) by smtp-server3.tampabay.rr.com (8.12.2/8.12.2) with ESMTP id h0FIU1tm007971 for ; Wed, 15 Jan 2003 13:30:01 -0500 (EST) From: "Matthew Nuzum" To: Subject: lost on self joins Date: Wed, 15 Jan 2003 13:29:47 -0500 Organization: Bearfruit.org Message-ID: <002d01c2bcc4$17e487a0$6700a8c0@mattspc> MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Priority: 3 (Normal) X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook, Build 10.0.3416 Importance: Normal X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2600.0000 X-Virus-Scanned: by AMaViS new-20020517 X-Archive-Number: 200301/227 X-Sequence-Number: 11092 Sometimes recursion makes my head spin... Imagine that I have a database that holds the structure of my filesystem. There is a table called files that contains every piece of info you would ever want to know about a file, including a unique ID called fileid. | files ======== x| fileid | filename | ... Then, you have a table called folders which looks like: | folders ========== x| folderid | parentid (relates to folders.folderid) | foldername Finaly, a table to allow a many to many join called files_folders | files_folders ================ x| ffid | folderid (fk to folders.folderid) | fileid (fk to files.fileid) Now, I'd like to create a view that shows everything in files, as well as the complete path to the file. However because I don't know how many levels deep the file is nested, I'm not sure how to get that complete path. Here is conceptually what should come out: | files_view ============== x| fileid | filename | ... | full_path Something that won't work is: SELECT files.*, folders.foldername, folders2.foldername FROM files, folders, folders folders2, files_folders ff WHERE files.fileid = ff.fileid AND ff.folderid = folders.folderid AND folders.parentid; The problem is that files that are not in a folder won't show up, and if a folder is more than two levels deep it will only show the two highest levels. Can anyone suggest a way for me to get the information I need? I'm very content to use a simple pl/pgsql function, however I don't know how I'd use recursion there. Thanks, -- Matthew Nuzum www.bearfruit.org cobalt@bearfruit.org