[GENERAL] left outer join on multi tables
Hi, here are my tables table name { id integer primary key first_name text, middle_name text, last_name text } table address { id integer primary key number int, street text, city text, state text } table work { id integer primary key hours text, shift } table person { id integer primary key namid integer references name(id), addressid integer referenes address(id), workid integer references work(id) } nameid, addressid or workid in person table may be empty. I would like to make a view which shows all information about a person in one row first_name, last_name, street, city, workhours, ... In the peron table, if the workid, addressid, or nameid is empty, just do not display the correspodent information. I think I need left outer join, but I do not know how to use it. Could anyone help? Thanks, qq ---(end of broadcast)--- TIP 2: Don't 'kill -9' the postmaster
[GENERAL] join table with empty fields and default
Hi, suppose I have two tables table User { id integer, name text, address text } Table UserWork { userid integer references User(id), work text } Suppose I have three users idnameaddress 1 Tony main street 2 Peter Big ave 3 Richard Loop Blvd And two of them work UserWork useridwork 1 programming 3 studying I would like to join table user and userwork, where if a user has a work, I list it. If he does not, I give it some default value "no work" Join User and UserWork idnameaddress work 1 Tony main streetprogramming 2 Peter Big ave no work 3 Richard Loop Blvd studying How can I write the sql? Thanks, qq ---(end of broadcast)--- TIP 3: Have you checked our extensive FAQ? http://www.postgresql.org/docs/faq
Re: [GENERAL] join table with empty fields and default
Thank you for your post. To make things a little bit complicated: Suppose I have another table UserInformation UserInfomation { userid integer referenes user(id), mothername text } I would like methername also appear in the final join. Suppose each user must have a mothername. Could you please whow me the sql again? Thanks again, qq Michael Fuhr wrote: > On Sat, Jul 22, 2006 at 12:47:42PM -0700, [EMAIL PROTECTED] wrote: > > I would like to join table user and userwork, where if a user has a > > work, I list it. If he does not, I give it some default value "no work" > > You could use an outer join and COALESCE. > > http://www.postgresql.org/docs/8.1/interactive/tutorial-join.html > http://www.postgresql.org/docs/8.1/interactive/queries-table-expressions.html#QUERIES-JOIN > http://www.postgresql.org/docs/8.1/interactive/functions-conditional.html#AEN12639 > > Example: > > SELECT u.id, u.name, u.address, COALESCE(uw.work, 'no work') AS work > FROM "user" AS u > LEFT OUTER JOIN userwork AS uw ON uw.userid = u.id; > > -- > Michael Fuhr > > ---(end of broadcast)--- > TIP 1: if posting/reading through Usenet, please send an appropriate >subscribe-nomail command to [EMAIL PROTECTED] so that your >message can get through to the mailing list cleanly ---(end of broadcast)--- TIP 5: don't forget to increase your free space map settings