简体   繁体   中英

select in mysql joining two tables with two instance of second table column

Sorry I really can't construct a good title, but let me elaborate it. :(

I have table report and table user_account .

report table have columns:

user_id | reported_user_id | date

user_account table have columns:

user_id | name | email

I need to get the name of users who reported and name of the user who was reported in one column.

It's like :

user_id | name | reported_user_id | name | email | date

I've tried left join, but I only have this:

user_id | reported_user_id | name | email

This is my statement:

select user_account.name, user_account.email, report.*
from report
left join user_account where user_account.user_id = report.reported_user_id;

Please enlighten my mind. Thank you.

select reporter.name as reporter_name, reported.name as reported_name
from report
left join user_account reporter on report.user_id = reporter.user_id
left join user_account reported on report.reported_user_id = reported.user_id

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM