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 |
|
Zander1983
Starting Member
3 Posts |
Posted - 2011-04-08 : 15:56:02
|
| Hiim creating a messages section on my site which will work the same as facebook messages i.e. with threadsI have the following tables:Message tableMessageID -> primary keyReplyToID -> this keep track of all messages that belong to the same threadFromID -> member id of senderToID -> member id of receiverMessage -> the text of the messsageDate -> date message was senti also have a member table containing Member ID and UsernameI would like to get back in one row something like this:MessageID, ReplyToID, FromID, ToID, Message, Date, FromUsername, SenderUsernamea simple join wont work as it returns 2 rows for every 1 row in the message table when it joins to member table....Any ideas? |
|
|
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts |
Posted - 2011-04-08 : 17:56:46
|
| Can you post the simple join that you tried? It may be that you are not joining on enough columns (or on too many columns) that is causing duplicate rows. |
 |
|
|
Zander1983
Starting Member
3 Posts |
Posted - 2011-04-08 : 18:21:20
|
| Hi this is the join im usingSELECT * from messages ms join member m on (m.MemberID = ms.FromID or m.MemberID = ms.ToID) WHERE ReplyToID = 8so for each row in messages, im going to get 2 rows, one with the username for FromID, and one with the username for ToID...Im sure i need to use a cursor here, i just dont know how.. |
 |
|
|
jeffw8713
Aged Yak Warrior
819 Posts |
Posted - 2011-04-08 : 19:10:37
|
| SELECT *FROM dbo.Messages msLEFT JOIN dbo.Member frID ON frID.MemberID = ms.FromIDLEFT JOIN dbo.Member toID ON toID.MemberID = ms.ToIDWHERE ms.ReplyToID = 8; |
 |
|
|
Zander1983
Starting Member
3 Posts |
Posted - 2011-04-08 : 19:39:22
|
| Thanks Jeff, this worked perfectly, its what i was looking for |
 |
|
|
jcelko
Esteemed SQL Purist
547 Posts |
Posted - 2011-04-08 : 19:57:10
|
| Please post real DDL. Learn to use ISO-11179 rules for the data element names, avoid needless dialect and use ISO-8601 temporal formats, codes and so forth. People cannot read your mind, so post your code and clear specs if you really want help. After a few decades of doing other people's jobs for them, why it is so much to ask them to just follow minimal, basic Netiquette? --CELKO--Books in Celko Series for Morgan-Kaufmann PublishingAnalytics and OLAP in SQLData and Databases: Concepts in Practice Data, Measurements and Standards in SQLSQL for SmartiesSQL Programming Style SQL Puzzles and Answers Thinking in SetsTrees and Hierarchies in SQL |
 |
|
|
|
|
|
|
|