| Author |
Topic |
|
doublek321
Starting Member
6 Posts |
Posted - 2005-07-15 : 23:47:03
|
| I have 2 tables:TABLE: MemberPK: idMemberNameTABLE: PortraitPK: idFK: memberIdpicURLPicIsUsedInProfileThe business rules are that a user can have between 0 to 3 pictures uploaded. However, one and only one of these pics can be used as the "profile pic". If the pic is used as the profile pic then the column is set to 1. If it's not used as the profile pic then it's either set to null or 0. My goal is to write a query that returns all member's (regardless of whether they have a pic or not) memberName as well as the picURL if they have a "profile pic" set (in other words column picIsUsedInProfile is set to 1). I'm not sure if this can be done in 1 query or a group of several queries. 1 query would be ideal but I'd be okay if it had to be written in several. I tried breaking it into several below and they both are okay. However, when I can't seem to return members who have 1 or more entries in the portraits table where NONE of these have a "profile pic" set. I know I can test for "isProfilePortrait = 0" but that's not good enough because I want to return a max of 1 row per member. In my scenario, however, a member can theoretically have 2 or 3 pics uploaded with none of them set to their "profile pic". I hope this makes sense. Any help would be GREATLY appreciated.-- "members with profile portraits"select m.*, p.*from member minner join portrait pon m.id = p.memberIdwhere p.isprofileportrait = 1-- "members with no portaits"select m.*from member mleft outer join portrait pon m.id = p.memberIdwhere p.id is null |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2005-07-16 : 01:13:31
|
| Can you post some sample data and expected result?MadhivananFailing to plan is Planning to fail |
 |
|
|
doublek321
Starting Member
6 Posts |
Posted - 2005-07-16 : 08:30:06
|
| Sure. The Member table is...ID MemberName1 Alan 2 Bob 3 Charlie 4 David 5 Ed 6 Fred 7 Gary 8 Henry 9 Irv 10 Jack 11 Kevin 12 Lenny 13 Morton 14 Nick 15 Orvis The portrait table is...ID MemberID PicURL IsProfilePortrait1 1 www.a.com 02 1 www.b.com 03 1 www.c.com 14 2 www.d.com 05 2 www.e.com 06 2 www.f.com 07 3 www.g.com 08 3 www.h.com 09 4 www.i.com 010 5 www.j.com 111 6 www.k.com 012 6 www.l.com 1I'm looking for this output (ideally in 1 query but if it had to be a union of several that would be ok too)...ID MemName PicURL 1 Alan www.c.com2 Bob NULL3 Charlie NULL4 David NULL5 Ed www.j.com 6 Fred www.l.com 7 Gary NULL8 Henry NULL9 Irv NULL10 Jack NULL11 Kevin NULL12 Lenny NULL13 Morton NULL14 Nick NULL15 Orvis NULL |
 |
|
|
mowafy
Starting Member
1 Post |
Posted - 2005-07-16 : 10:40:43
|
| i think you can use an left outer join statmentthat select * records from member an portrait |
 |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2005-07-16 : 10:41:24
|
"Query Question" -- isn't that redundant? - Jeff |
 |
|
|
doublek321
Starting Member
6 Posts |
Posted - 2005-07-16 : 12:18:56
|
| An outer join would work but it would return duplicates. Below are 2 examples of outer joins. Both return dupes. I just want each memberName to show up once and only once.------------------------------------------------OUTER JOIN EXAMPLE 1: select m.id, m.memberName, p.picURL, p.IsProfilePortraitfrom member mleft outer join portrait pon m.id = p.memberId1 Alan www.a.com 01 Alan www.b.com 01 Alan www.c.com 12 Bob www.d.com 02 Bob www.e.com 02 Bob www.f.com 03 Charlie www.g.com 03 Charlie www.h.com 04 David www.i.com 05 Ed www.j.com 16 Fred www.k.com 06 Fred www.l.com 17 Gary NULL NULL8 Henry NULL NULL9 Irv NULL NULL10 Jack NULL NULL11 Kevin NULL NULL12 Lenny NULL NULL13 Morton NULL NULL14 Nick NULL NULL15 Orvis NULL NULL------------------------------------------------------------------------------------------------OUTER JOIN EXAMPLE 2:select m.id, m.memberName, p.picURL, p.IsProfilePortraitfrom member mleft outer join portrait pon m.id = p.memberIdwhere p.isProfilePortrait = 01 Alan www.a.com 01 Alan www.b.com 02 Bob www.d.com 02 Bob www.e.com 02 Bob www.f.com 03 Charlie www.g.com 03 Charlie www.h.com 04 David www.i.com 06 Fred www.k.com 0------------------------------------------------- |
 |
|
|
SamC
White Water Yakist
3467 Posts |
Posted - 2005-07-16 : 13:44:39
|
quote: Originally posted by doublek321 My goal is to write a query that returns all member's (regardless of whether they have a pic or not) memberName as well as the picURL if they have a "profile pic" set (in other words column picIsUsedInProfile is set to 1).
SELECT M.MemberName, P.picURL FROM Member M LEFT OUTER JOIN Portrait P ON P.memberId = M.id AND P.PicIsUsedInProfile = 1 -- Only join "profile pic" rows LEFT JOIN on PicIsUsedInProfile will produce one row per member (as long as PicIsUsedInProfile is only set to 1 once for each member. Members with no picture or no profile pic will return column picURL set to NULL. |
 |
|
|
doublek321
Starting Member
6 Posts |
Posted - 2005-07-16 : 15:54:04
|
| Sam C, that doesn't cover the case though where a user has a record (or multiple records) in the Portrait table where *none of them* have column PicIsUsedInProfile=1. The Member with id 2 is such a case. He has 3 entries in the Portrait table but none of them have PicIsUsedInProfile=1. So this Member would not return as part of the query results. And I need *all* members to return (once and only once). |
 |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2005-07-16 : 17:38:56
|
| doublek -- did you try Sam's query?- Jeff |
 |
|
|
doublek321
Starting Member
6 Posts |
Posted - 2005-07-16 : 17:42:24
|
| Whoops, it totally does work. SamC, thanks a bunch for your help. |
 |
|
|
|