Results 1 to 2 of 2

Thread: Correct SQL statement?

  1. #1

    Thread Starter
    Hyperactive Member oyad's Avatar
    Join Date
    Feb 2003
    Location
    PhoxWare MicroSystems
    Posts
    463

    Correct SQL statement?

    plz,i cant figure out the right SQL to retrieve what i want from my SQL server 2005 database.
    I have two tables, Table 1 is named tUsers & its columns hav the from (UserId,SName,FName) while Table 2 is named tData with columns (Date,Msg,Pass, UserId1,UserId2). Both UserId1 and UserId2 of tData table depends on UserId of tUsers table. I want to retrieve each row from tData of the form : Date, Msg, Pass, UserId1,SName of UserId1, FName of UserId1, UserId2,SName of UserId2, FName of UserId2)

    Am getting some unnecessary results from my joins, any person to help the right SQL
    10q
    Nobody is smarter than all of us!

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Correct SQL statement?

    You just need to use two "instances" of the tUsers table, eg:
    Code:
    SELECT [Date], Msg, Pass, UserId1, U1.SName, U1.FName, UserId2, U2.SName, U2.FName
    FROM tData
    LEFT JOIN tUsers U1 ON (tData.UserID1 = U1.UserID)
    LEFT JOIN tUsers U2 ON (tData.UserID2 = U2.UserID)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width