sql - Count twice if certain condition satisfies in mysql -


i have scenario need display total number of attendees of event. of registration form have captured details of people attending , table looks below.


id     | name     | phone_number     | is_life_partner_attending

1      | abc      | 1234567890       | n                          

2      | pqr      | 1234567891       | y                          

3      | xyz      | 1234567892       | n                          

i can display number of registrations using count(id). while displaying number of attendees have consider 2 attendees if registrant coming his/her partner. (identified is_life_partner_attedning column)

so, in above case, number of registrants 3, number of attendees 4, because "pqr" coming his/her life partner.

how can in mysql query?

you can use following query:

select  sum( 1 + (is_life_partner_attedning = 'y')) totalattendees your_table; 

working demo

since boolean expression resolves 0/1 in mysql can capitalize in case.

note:

sum(a=b) returns 1 if equal b otherwise returns 0

caution:

*never underestimate these parentheses (is_life_partner_attedning = 'y'). if omit them whole summation result in zero(0).

* because of operator precedence


Comments