Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
phive
Starting Member
3 Posts |
Posted - 2006-12-09 : 00:30:15
|
Hi all, What I'm trying to do and having a lot of trouble with is pulling how one user is related to another user from my database. I'll explain...The table (relationships) looks like this:IDtype: int RELID1type: int, is the user id that initiated the relationship request. RELID2type: int, is the user id of the second person in the relationship. Storytype: varchar(255), quick blerb on how they are related. Typetype: int, a number 1-20 based on the relationship they have Statustype: int, 1 = confirmed by second person, 0 = not confirmedWhat i'd like to have pulled is a list of the people that person (for example: 70) is related to. I am having the two following problems:1. unqid of "70" could be in RELID1 or RELID2 as they could have initiated the request or been the second person.2. I don't want to display them selves in their own relationship listingExample Data:ID1234 RELID125157012 RELID25470138 StoryMet on the east coastMet at walmartMet walkingMet outside Type141138 Status1111Example Output:Again assuming the current user is "70", the sql should pull: 15, 13.. but i'd like to pull their names from another table called "Users" where "15" and "13" are the UnqID's in a column called "ID".So:15 = Bob Smith13 = Jane DOeWhat I've Tried:SELECT RELID1, RELID2, FirstName, LastNameFROM RelationshipINNER JOIN ReMembersON Relationship.RELID1 = ReMembers.AccountIDWHERE RELID1 = 70 OR RELID2 = 70 This works, however, this only works for RELID1 and not RELID2 column. Moreover, this repeats the data for some reason.If you have any more questions, or something isn't clear please let me know!Thanks!~ PhIve ~ |
|
|
Vinnie881
Master Smack Fu Yak Hacker
1231 Posts |
Posted - 2006-12-09 : 01:42:24
|
| This is not the proper way to setup this type of database, however you can make it work. select Distinct a.* from (SELECT RELID1, RELID2, FirstName, LastNameFROM RelationshipINNER JOIN ReMembersON Relationship.RELID1 = ReMembers.AccountIDunion allSELECT RELID1, RELID2, FirstName, LastNameFROM RelationshipINNER JOIN ReMembersON Relationship.RELID2 = ReMembers.AccountID) aWHERE a.RELID1 = 70 OR a.RELID2 = 70 |
 |
|
|
phive
Starting Member
3 Posts |
Posted - 2006-12-09 : 02:11:05
|
| Thank you so much!Its funny that you say that, see our database guy is on vacation until next Monday and I'm just the the web developer. As fate would have it, we have to do a presentation on Monday so I'll be working this weekend getting a few things working.My next question I guess would be, how would you suggest setting this up?Two tables? or have it in as a string in the ReMembers table?At this point any help I can get is MORE then appreciated! |
 |
|
|
phive
Starting Member
3 Posts |
Posted - 2006-12-09 : 13:04:48
|
| RESOLVED![url]http://forums.devshed.com/ms-sql-development-95/arg--relationship-between-two-members-408574.html[/url] |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-12-10 : 17:05:32
|
Thank you for occupying more people than necessary for solving this matter!-- prepare test datadeclare @test table (ID int, RELID1 int, RELID2 int, Story varchar(255), Type int, Status int)insert @testselect 1, 25, 54, 'Met on the east coast', 14, 1 union allselect 2, 15, 70, 'Met at walmart', 11, 1 union allselect 3, 70, 13, 'Met walking', 3, 1 union allselect 4, 12, 8, 'Met outside', 8, 1-- do the workselect u.*from ( select relid1 relid from @test where relid2 = 70 and relid1 <> relid2 union select relid2 from @test where relid1 = 70 and relid2 <> relid1 ) qinner join users u on u.unqid = q.relid Peter LarssonHelsingborg, Sweden |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
|
|
|
|
|
|
|