我有一个这样的用户表:
Id --- Privilege1 --- Privilege2 --- Privilege3 --- Privilege4 --- Privilege-5
1 1 0 0 1 0
4 0 1 0 0 0
12 1 1 1 1 0
ID
包含用户ID,Privilege1 - Privilege5
包含值0
(否)或1
(是)。
我需要获取大多数权限为1
的行的ID。在我的示例中它应该是12,因为该行具有最多1's
。我怎么能这样做?
答案 0 :(得分:-1)
SELECT `id`, (`Privilege1` + `Privilege2` + `Privilege3` + `Privilege4` + `Privilege5`) AS `priv`
FROM `table`
HAVING `priv` = MAX(`Privilege1` + `Privilege2` + `Privilege3` + `Privilege4` + `Privilege5`)