Alpenglow
Alpenglow

Reputation: 173

MySQL Coalesce() function - Determining which field was non-null

I am writing a query that fetches the permissions for a user on our website. It first checks to see if there are any overrides for the permissions, then checks the permissions for the users role on the website, and if those are null it's defaulted to 0 (AKA no permission.. the other values would be >1). I figured coalesce() would be the best choice here as I could do something like

coalesce(override, role, 0);

Where override and role would be the fields in the database.

However, if it was indeed an override permission, I need to know because I display an icon indicating it as such. Is there any way I could determine this in a clean manner?

Upvotes: 1

Views: 118

Answers (1)

DNadel
DNadel

Reputation: 485

Add a separate column with boolean for an override.

select coalesce(override,role,0), (override is not null) as override_enabled

Upvotes: 3

Related Questions