每次仅获得两列时,我都无法获取团队ID和user_type
SELECT `dev_user`.first_name,`dev_user`.user_id FROM `dev_test_teams_athlete` LEFT JOIN dev_user ON dev_test_teams_athlete.user_id = dev_user.user_id UNION ALL SELECT `dev_user`.first_name, `dev_test_team`.user_id as coach_id FROM `dev_user` RIGHT JOIN dev_test_team ON dev_test_team.user_id = dev_user.user_id UNION ALL SELECT dev_test_team.team_id,dev_test_teams_athlete.user_id as athlete FROM `dev_test_team` LEFT JOIN `dev_test_teams_athlete` ON `dev_test_team`.`team_id` = `dev_test_teams_athlete`.`team_id`
这是在查询中使用全部联盟关键字
的结果我得到的结果类似不需要的结果enter image description here
我的dev_test_team表是enter image description here
我的dev_test_teams_athlete表是enter image description here
我的dev_user表是enter image description here
最后,我要得到图像enter image description here
中所述的结果第一个我想这样回答
-------------------------------------------
user_id | first_name | user_type | team_id
-------------------------------------------
480 coach1 3 1
472 coach2 3 2
465 coach3 3 3
380 athlete1 4 1
375 athlete2 4 2
479 athlete3 4 3
464 athlete4 4 3
答案 0 :(得分:0)
SELECT
`x`.`team_id`, `du`.`first_name`, `du`.`user_type`, `x`.`team_id`
FROM(
SELECT `team_id`, `user_id` FROM `dev_test_team`
UNION ALL
SELECT `team_id`, `user_id` FROM `dev_test_teams_athlete`
) `x`
LEFT JOIN `dev_user` `du` ON `du`.`user_id` = `x`.`user_id`
================================================ ============
尝试一下。