Solution to the Problem of associating two tables with the same ID of the third table during multi-table join query

Source: Internet
Author: User

Solution to the Problem of associating two tables with the same ID of the third table during multi-table join query

 

 

It is better to use examples to illustrate the problem. You can refer to the diagram and code below to help understand the problem.

 

Just add an alias reference for the bbsusers table based on the original one. The problem is also very simple. I will not talk about it much!

 

 

The database relationship diagram is as follows:

 

 

 

The Code is as follows:

 

-- Query and display the title, author name, content, title, name, content, and time of the primary post based on the fixed id value.
Select T. ttopic as 'Post title', tu. uname as 'author name', T. tcontents as 'content ',
R. rtopic as 'Post title', Ru. uname as 'Post author name', R. rcontents as 'Post content', R. rtime as 'Post time'
From bbstopic as t, bbsreply as R, bbsusers as Tu, bbsusers as Ru
Where T. tid = R. RTID and Tu. uid = T. tuid and Ru. uid = R. ruid and T. tid = 2
Order by R. rtime
-- Or:
Select T. ttopic as 'Post title', tu. uname as 'author name', T. tcontents as 'content ',
R. rtopic as 'Post title', Ru. uname as 'Post author name', R. rcontents as 'Post content', R. rtime as 'Post time'
From bbstopic as t
Inner join bbsreply as R
On T. tid = R. RTID
Inner join bbsusers as Tu
On tu. uid = T. tuid
Inner join bbsusers as Ru
ON Ru. uid = R. ruid
Where T. tid = 2
Order by R. rtime

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.