我正试图在没有运气的情况下实现MySQL版本的FULL JOIN。我的结果需要包含来自“用户”的所有行,无论用户是否在其他表中具有匹配的行。现在我的结果不是全部由于LEFT JOIN的性质,但是当我UNION ALL,LEFT和RIGHT时,我的结果进一步缩小了。
SELECT
users.*, profile_status.*, photos.*, general.*, experience.*, occupation.*,
flagged.*, last_update.*, job_search.*, users.user_id AS userID,
user_profile_status.user_id AS profileID
FROM users
JOIN profile_status ON users.user_id = profile_status.user_id
LEFT JOIN photos ON users.user_id = photos.user_id AND photos.selected = 1
LEFT JOIN general ON users.user_id = general.user_id
LEFT JOIN occupation ON users.user_id = occupation.user_id
LEFT JOIN experience ON users.user_id = experience.user_id
LEFT JOIN flagged ON users.user_id = flagged.user_id
LEFT JOIN job_search ON users.user_id = job_search.user_id
LEFT JOIN last_update ON users.user_id = last_update.user_id
WHERE
users.approved = 1 AND users.admin_level = 0
ORDER BY last_update.last_update DESC
我对UNION的尝试
SELECT
users.*, profile_status.*, photos.*, general.*, experience.*, occupation.*,
flagged.*, last_update.*, job_search.*, users.user_id AS userID,
user_profile_status.user_id AS profileID
FROM users
JOIN profile_status ON users.user_id = profile_status.user_id
LEFT JOIN photos ON users.user_id = photos.user_id AND photos.selected = 1
LEFT JOIN general ON users.user_id = general.user_id
LEFT JOIN occupation ON users.user_id = occupation.user_id
LEFT JOIN experience ON users.user_id = experience.user_id
LEFT JOIN flagged ON users.user_id = flagged.user_id
LEFT JOIN job_search ON users.user_id = job_search.user_id
LEFT JOIN last_update ON users.user_id = last_update.user_id
UNION ALL
SELECT
users.*, profile_status.*, photos.*, general.*, experience.*, occupation.*,
flagged.*, last_update.*, job_search.*, users.user_id AS userID,
user_profile_status.user_id AS profileID
FROM users
JOIN profile_status ON users.user_id = profile_status.user_id
RIGHT JOIN photos ON users.user_id = photos.user_id AND photos.selected = 1
RIGHT JOIN general ON users.user_id = general.user_id
RIGHT JOIN occupation ON users.user_id = occupation.user_id
RIGHT JOIN experience ON users.user_id = experience.user_id
RIGHT JOIN flagged ON users.user_id = flagged.user_id
RIGHT JOIN job_search ON users.user_id = job_search.user_id
RIGHT JOIN last_update ON users.user_id = last_update.user_id
WHERE
users.approved = 1 AND users.admin_level = 0
ORDER BY last_update.last_update DESC
表格结构:
Table users
user_id first_name last_name admin_level user_approved
1 tommy jones 0 1
2 johnny rocket 0 1
3 sally hotpants 0 1
Table profile_status
user_id photo_uploaded
1 1
2 1
3 1
Table photos
photo_id user_id filename selected
1 1 photo_1.jpg 1
2 2 photo_2.jpg 0
3 2 photo_2.jpg 1
4 3 photo_3.jpg 1
Table general
user_id city state zip neighborhood
1 baltimore maryland 00125 hamsterdam
2 lakeland maine 11542 treemont
2 tipper vermont 08483 NULL
Table experience
user_id waldorf kumon homeschooling
1 0 1 0
2 0 0 1
Table occupation
user_id occupation
1 teacher
2 student
3 student
Table job_search
search_id user_id job
1 1 art teacher
2 2 designer
Table flagged
user_id flagged
1 1
2 0
Table last_update
update_id user_id last_update photo_update
1 1 0000-00-00 00:00:00 0000-00-00 00:00:00
2 2 0000-00-00 00:00:00 0000-00-00 00:00:00