Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id 81A24475843 for ; Wed, 15 Jan 2003 22:26:05 -0500 (EST) Received: from megazone.bigpanda.com (megazone.bigpanda.com [63.150.15.178]) by postgresql.org (Postfix) with ESMTP id 00C37475CC4 for ; Wed, 15 Jan 2003 22:26:04 -0500 (EST) Received: by megazone.bigpanda.com (Postfix, from userid 1001) id 3993ED60D; Wed, 15 Jan 2003 19:26:08 -0800 (PST) Received: from localhost (localhost [127.0.0.1]) by megazone.bigpanda.com (Postfix) with ESMTP id 2F4D85C02; Wed, 15 Jan 2003 19:26:08 -0800 (PST) Date: Wed, 15 Jan 2003 19:26:08 -0800 (PST) From: Stephan Szabo To: Matthew Nuzum Cc: Subject: Re: show data from two tables together In-Reply-To: <003d01c2bcfa$d2084b40$6700a8c0@mattspc> Message-ID: <20030115191320.R98147-100000@megazone23.bigpanda.com> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-Virus-Scanned: by AMaViS new-20020517 X-Archive-Number: 200301/247 X-Sequence-Number: 11112 On Wed, 15 Jan 2003, Matthew Nuzum wrote: > Well, this is somewhat of a follow up to my previous post regarding self > joins. Now what I'm hoping to do is "virtually" combine the results > from two different record sets into one apparent record set. Fortunately we have the set functions, specifically UNION ALL in this case. > Here is the skeleton of my application's data structure. There is a > table called "folders" and a table called "files". > > They look like: > | files | folders > ============= ============ > x| fileid x| folderid > | filename | foldername > | folderid | parentid > | dsply_order | dsply_order > > files.folderid is fk to folders.folderid, folders.parentid is field for > self joining to folderid. > > As a side note, I'd probably need to add a field that would indicate 1 > if the file came from files otherwise count(folders.*) WHERE parentid = > folderid so that I can see if the folder is empty. > > As another side note, this operation will be performed quite frequently > and should be fast. > > As I think about it, it seems that the only logical way would be to do > this at the application level, not inside postgres. Please correct me > if I'm wrong. Maybe something like (minus the number of files/empty part): CREATE VIEW viewname AS SELECT fileid, filename, folderid, dsply_order FROM files UNION ALL SELECT folderid, foldername, parentid, dsply_order FROM folders; SELECT * from viewname where folderid=23 order by dsply_order; Depending on whether you want a count of files or just an empty or not, and whether you want info on whether a particular entry is a file or folder, you'll probably need to add to the above.