MySQL count a boolean field as two different columns -
think have table 2 fields: id , state. state value (that boolean) can 0 or 1. id isn't unique table looks this:
id | state | ----------------- 1 | true | ----------------- 1 | false | ----------------- 2 | false | ----------------- 3 | true | ----------------- 1 | true | now, want count every rows group id field , have state 2 different columns in resultset. should this:
id | truestate | falsestate | ------------------------------------ 1 | 2 | 1 | ------------------------------------ 2 | 0 | 1 | ------------------------------------ 3 | 1 | 0 | how that?
this pivot query, mysql doesn't support. workarounds ugly fast, since you're going generating 2 new columns, won't horribly ugly, mildly unpleasant:
select sum(state = true) truestate, sum(state = false) falsestate, sum(state null) filenotfoundstate ... basically state = true evaluate boolean true/false, mysql type-cast integer 0 or 1, can them sum()med up.
Comments
Post a Comment