Joining the Same Table Twice on Different Columns
Question:
A user and complaint table are established, where users can raise and resolve complaints. The complaint table includes the user_id of both the complainant and the resolver. The goal is to write a query that displays the usernames of both parties instead of their user_ids.
Answer:
To join the user table twice on different columns and retrieve the usernames, the following query can be used:
SELECT complaint.complaint_text, A.username AS OpenedBy, B.username AS ClosedBy FROM complaint LEFT JOIN user A ON A.user_id=complaint.opened_by LEFT JOIN user B ON B.user_id=complaint.closed_by
In this query:
The above is the detailed content of How to Join a Table Twice on Different Columns to Display Usernames Instead of User IDs?. For more information, please follow other related articles on the PHP Chinese website!