我认为您需要扩展子查询以使用特定条件过滤掉重复的用户。例如,如果您想选择具有最大 user_id 的用户,则查询将是:
SELECT
a.id, a.name,
b.id as mixer_user_id,
b.first_name as mixer_first, b.last_name as mixer_last,
c.id as baker_user_id,
c.first_name as nbaker_first, c.last_name as baker_first,
d.id as decorator_user_id,
d.first_name as decorator_first, d.last_name as decorator_last,
a.active
FROM
orders a
JOIN (
SELECT b1.id as id, b1.first_name, b1.last_name, b2.order_id
FROM users b1, order_users b2
WHERE b1.id=b2.user_id and b2.user_type='mixer'
AND b1.id = (SELECT MAX(user_id) FROM order_users WHERE order_id = b2.order_id AND user_type = b2.user_type)
) b ON (a.id = b.order_id)
JOIN (
SELECT c1.id as id, c1.first_name, c1.last_name, c2.order_id
FROM users c1, order_users c2
WHERE c1.id=c2.user_id and c2.user_type='baker'
AND c1.id = (SELECT MAX(user_id) FROM order_users WHERE order_id = c2.order_id AND user_type = c2.user_type)
) c ON (a.id = c.order_id)
JOIN (
SELECT d1.id as id, d1.first_name, d1.last_name, d2.order_id
FROM users d1, order_users d2
WHERE d1.id=d2.user_id and d2.user_type='decorator'
AND d1.id = (SELECT MAX(user_id) FROM order_users WHERE order_id = d2.order_id AND user_type = d2.user_type)
) d ON (a.id = d.order_id)
WHERE
a.id in (1001, 1002, 1003);
如果您有不同的 RDBMS(例如 Oracle 或 MSSQL),您将使用 CTE 创建一个分组的 order_users 表。但只要你坚持使用 MySQL,你就别无选择在子查询中使用它。
如果你经常使用这个查询,你应该考虑创建 VIEW