MySQL 查询:select 在 table 中全部处于活动状态,但接受 id = 1 的非活动状态

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

我知道这个不行,但我试一试,在同一个 table.

上使用 union
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 

再试一次

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

可能吗?

您的所有选择都处于活动状态,但具有指定 ID 的一项处于非活动状态

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

只需在 sys_id = 1

中添加条件
select  `sys_id`,`username`,`status`
    from  user_tbl
    WHERE  status='active' ||
          ( sys_id=1   and  `status`='inactive' )
    ORDER by  sys_id

所有inactive OR (inactive with id 1)都可以这样实现:

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