zeyang song
zeyang song

Reputation: 79

MySQL outputs multiple columns per field based on field status

I have a MySQL(version:5.7) table with the following fields:

 account: varchar
 work_status: varchar
 work_details: varchar

There are data like this:

 account       |   work_status   |   work_details |  
 [email protected]        work          coding
 [email protected]       onBusiness    conclude a contract 

I want to achieve something similar to the following:

 account       |   work_status_work   |  work_status_onBusiness |  
 [email protected]       22                   8

Upvotes: 2

Views: 58

Answers (1)

Fahmi
Fahmi

Reputation: 37473

You can try using conditional aggregation -

select account, 
       count(case when work_status='work' then 1 end) as work_status_work,
       count(case when work_status='onBusiness' then 1 end) as work_status_onBusiness
from tablename
group by account

Upvotes: 3

Related Questions