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