SQL one record multiple fields corresponding to another table to describe the wording

Source: Internet
Author: User
Tags joins

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.