I am trying to run this as a single query, but with the duplicated sub queries only being executed once, I thought it may be possible with a JOIN clause but I am not sure if it is feasible as Im looking up the name and role field from the same row 3 times? The only way I can think to do it is to execute the sub query once from my code to get the role and name values to inject into this main query.
SELECT id, isActive, role
FROM accountMembers
WHERE account=:acc
AND isActive=true
AND (
(
name > (SELECT name FROM accountMembers WHERE account=:acc AND id =:id)
AND role = (SELECT role FROM accountMembers WHERE account=:acc AND id =:id)
)
OR role > (SELECT role FROM accountMembers WHERE account=:acc AND id =:id)
)
ORDER BY role ASC, name ASC LIMIT :lim