此查询返回所有联系人ID和姓名,但没有特定顺序。我想按名称排序结果。...
如果我在括号内添加“按u.name排序”,即使在联合的两个部分上,内部联接部分似乎也无法阻止查询。
(select distinct u.user_id, u.name from users u
inner join companies_users cu on cu.user_id = u.user_id
where c.company_id = ? and u.status='active' and c.status='active')
union
(select distinct u.user_id, u.name from users u
inner join locations_users lu on lu.user_id = u.user_id
inner join locations l on l.location_id = lu.location_id
where l.company_id = ? and u.status='active' and l.status='active')