我有一个庞大的 MySQL 查询,它依赖于 JOIN。
SELECT m.id, l.name as location, CONCAT(u.firstName, " ", u.lastName) AS matchee, u.email AS mEmail, u.description AS description, m.time AS meetingTime
FROM matches AS m
LEFT JOIN locations AS l ON locationID=l.id
LEFT JOIN users AS u ON (u.id=m.user1ID)
WHERE m.user2ID=2
UNION
SELECT m.id, l.name as location, CONCAT(u.firstName, " ", u.lastName) AS matchee, u.email AS mEmail, u.description AS description, m.time AS meetingTime
FROM matches AS m
LEFT JOIN locations AS l ON locationID=l.id
LEFT JOIN users AS u ON (u.id=m.user2ID)
WHERE m.user1ID=2
每个子语句的前 3 行除以 UNION 是相同的。如何遵守 DRY 原则,不重复这三行,让这个查询更简洁?