我有这张桌子

create table votes(
    candidate varchar(20),
    voteup bool
)

我想展示这样的东西
|candidate|vote ups(true)|vote downs(false)|vote points(true-false)
|hitler   |16            |104              |-88
|obama    |119           |1                |118

最佳答案

您可以“滥用”布尔值在整数运算中计算为1和0的事实:

select candidate,
    sum(voteup) as 'vote up',
    sum(voteup=0) as 'vote down',
    sum(if(voteup,1,-1)) as 'vote points'
from votes
group by candidate;

关于mysql - 如何在MySQL上分隔 bool 列值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28379069/

10-11 05:21