Skip to main content

If I have a MySQL table looking something like this:

 company_name action pagecount ------------------------------- Company A PRINT 3 Company A PRINT 2 Company A PRINT 3 Company B EMAIL Company B PRINT 2 Company B PRINT 2 Company B PRINT 1 Company A PRINT 3 
company_nameactionpagecount
Company APRINT3
Company APRINT2
Company APRINT3
Company BEMAIL
Company BPRINT2
Company BPRINT2
Company BPRINT1
Company APRINT3

Is it possible to run a MySQL query to get output like this:

 company_name EMAIL PRINT 1 pages PRINT 2 pages PRINT 3 pages ------------------------------------------------------------- CompanyA 0 0 1 3 CompanyB 1 1 2 0 
company_nameEMAILPRINT 1 pagesPRINT 2 pagesPRINT 3 pages
CompanyA0013
CompanyB1120

The idea is that pagecount can vary so the output column amount should reflect that, one column for each action/pagecount pair and then number of hits per company_name. I'm not sure if this is called a pivot table but someone suggested that?

If I have a MySQL table looking something like this:

 company_name action pagecount ------------------------------- Company A PRINT 3 Company A PRINT 2 Company A PRINT 3 Company B EMAIL Company B PRINT 2 Company B PRINT 2 Company B PRINT 1 Company A PRINT 3 

Is it possible to run a MySQL query to get output like this:

 company_name EMAIL PRINT 1 pages PRINT 2 pages PRINT 3 pages ------------------------------------------------------------- CompanyA 0 0 1 3 CompanyB 1 1 2 0 

The idea is that pagecount can vary so the output column amount should reflect that, one column for each action/pagecount pair and then number of hits per company_name. I'm not sure if this is called a pivot table but someone suggested that?

If I have a MySQL table looking something like this:

company_nameactionpagecount
Company APRINT3
Company APRINT2
Company APRINT3
Company BEMAIL
Company BPRINT2
Company BPRINT2
Company BPRINT1
Company APRINT3

Is it possible to run a MySQL query to get output like this:

company_nameEMAILPRINT 1 pagesPRINT 2 pagesPRINT 3 pages
CompanyA0013
CompanyB1120

The idea is that pagecount can vary so the output column amount should reflect that, one column for each action/pagecount pair and then number of hits per company_name. I'm not sure if this is called a pivot table but someone suggested that?

edited tags
Link
Rick James
  • 143.6k
  • 15
  • 144
  • 255
edited tags
Link
Barbaros Özhan
  • 65.9k
  • 11
  • 36
  • 64
edited title
Link
TylerH
  • 21.3k
  • 84
  • 84
  • 121
Loading
Rollback to Revision 3
Source Link
Taryn
  • 248.9k
  • 57
  • 374
  • 409
Loading
added 472 characters in body
Source Link
m87
  • 4.5k
  • 3
  • 18
  • 31
Loading
Question Protected by Samuel Liew
Question Unprotected by Gordon
Question Protected by M Khalid Junaid
Fixed formatting.
Source Link
user212218
user212218
Loading
alter the format of the desired table output for legibility
Source Link
Loading
Source Link
peku
  • 5.1k
  • 3
  • 22
  • 15
Loading