bdalina bdalina - 1 month ago 7
MySQL Question

MySQL query: select all active in a table but accept an inactive which id = 1

I know this one won't work but i give it a try, using union on the same table.

select `sys_id`,`username`,`status`
from user_tbl
WHERE sys_id=1
UNION ALL
SELECT `sys_id`,`username`,`status`
from user_tbl
WHERE status ='active'
ORDER by sys_id


another try

select `sys_id`,`username`,`status`
from user_tbl
WHERE status='active' || sys_id=1
ORDER by sys_id


Is it possible?

all your selection is active but one item is inactive with an specified Id

1 | inactive
2 | active
3 | active
4 | active

Answer

All inactive OR (inactive with id 1) can be achieved like this:

select `sys_id`,`username`,`status` from user_tbl WHERE status='active' or
(status='inactive' and sys_id=1) ORDER by sys_id