pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Dominik Bruhn <dominik@dbruhn.de>
To: pgsql-jdbc@postgresql.org
Subject: Retrieving Columns by table-alias
Date: Thu, 30 Aug 2007 22:44:38 +0200
Message-ID: <46D72C36.9010309@dbruhn.de> (raw)

Hy,
I used MySQL for years and now wanted to try out something new (and
perhaps better).

Assume the following Tables:

post:
	postid: serial
	postuserid: integer
	edituserid: integer

user:
	userid: serial
	username: varchar

Every "post" got two Users accociated to them, the onw who wrote it at
first time (postuserid) and the last user who edited it (edituserid). So
get a list of all post I use the following query:

SELECT * FROM post
	LEFT JOIN user AS postuser ON (post.postuserid=postuser.userid)
	LEFT JOIN user AS edituser ON (post.edituserid=edituser.userid)


This works fine until I want to retrieve the columns in JDBC:
As "userid" is not a unique label (could be postuser.userid and
edituser.userid) I need another solution.

When using mysql I could write:
ResultSet.getString("postuser.username") for example, so prefix the
column with the table-alias.

This syntax doesnt seem to be avaliable.

Is there another solution?

The only thing I could think of was to name all columns in the query
like this:

SELECT
	post.*,
	postuser.userid AS postuser_userid,
	postuser.username AS postuser_username,
	edituser.userid AS edituser_userid,
	edituser.username AS edituser_username
FROM post
	LEFT JOIN user AS postuser ON (post.postuserid=postuser.userid)
	LEFT JOIN user AS edituser ON (post.edituserid=edituser.userid)

BUT: This solution isnt what I want: Everytime I add another column to
the user-table, I have to rewrite ALL Queries in Order to retrieve the
data. I simply want it to access in the Result-Set.

Can anybody give me a hint?

Thanks in advance
Dominik Bruhn



view thread (2+ messages)  latest in thread

Message-ID: <46D72C36.9010309@dbruhn.de>
Permalink:  ../46D72C36.9010309@dbruhn.de/
Also on:    postgresql.org/message-id/46D72C36.9010309@dbruhn.de

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-jdbc@postgresql.org
  Cc: dominik@dbruhn.de
  Subject: Re: Retrieving Columns by table-alias
  In-Reply-To: <46D72C36.9010309@dbruhn.de>
  References: <46D72C36.9010309@dbruhn.de>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox