Users table:
Order Form:
Now in the Orders table, the creator and the update person are the primary key userid that corresponds to the users table, and when you query the order, you want to display the user name
1. First give your usual wording
Select O.orderid,
IsNull ((select top 1 UserName from Users where userid=o.createpeople), Createpeople,--Creator
IsNull ((select top 1 UserName from Users where userid=o.updatepeople), Updatepeople,--New
O.amount
From Orders o
2. Another notation
Select
O.orderid,
U1. UserName createpeople,--Creator
U2. UserName updatepeople,--More New
O.amount
From Orders o
Left joins Users U1 on O.createpeople=u1. Userid
Left joins Users U2 on O.updatepeople=u2. Userid
ORDER BY O.orderid
Do the following
As above, the effect is identical (the last line is inconsistent because the IsNull function of the first notation has been processed, not isnull () exactly the same)
Get here first.
SQL one record multiple fields corresponding to another table to describe the wording