2

I'm trying to show a list of users where the list of accounts which are activated=1 are below while activated=0 accounts are on top. I tried using UNION since I need all inactive accounts as ORDER BY created DESC while activated accounts set as ORDER BY email but for some reason all my ORDER BY commands are being ignored. What am I doing wrong here?

(
    SELECT 
        email, 
        roles.full role, 
        created 
    FROM sol_users users
    JOIN sol_user_roles ur ON users.id = ur.user_id 
    JOIN sol_roles roles USING(role_id) 
    WHERE activated = 0
    ORDER BY created DESC
)
UNION
(
    SELECT
        email, 
        roles.full role, 
        created
    FROM sol_users users
    JOIN sol_user_roles ur ON users.id = ur.user_id 
    JOIN sol_roles roles USING(role_id) 
    WHERE activated = 1
    ORDER BY email
)

Inactive users need to be placed on top so the Admin will know that these need to be activated.

2 Answers 2

2

You don't need a union - try:

SELECT email, roles.full role, created 
FROM sol_users users
JOIN sol_user_roles ur ON users.id = ur.user_id 
JOIN sol_roles roles USING(role_id) 
ORDER BY activated, case activated when 1 then email end, created DESC
Sign up to request clarification or add additional context in comments.

1 Comment

Woah. I never knew you could use conditionals for ORDER BY. Such beautiful code!
0

You can try to use an IF statement in your ORDER BY clause. See this for a discussion about a similar topic.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.