Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9143389
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T10:00:44+00:00 2026-06-17T10:00:44+00:00

Possible Duplicate: MySQL pivot row into dynamic number of columns I have a query

  • 0

Possible Duplicate:
MySQL pivot row into dynamic number of columns

I have a query that shows number of items per day

$query = "SELECT uploadedby, count(item) as item, date_format(uploaddate, '%m-%d-%y') as date FROM `imsexport` GROUP BY uploadedby,date";
$result = mysql_query($query);

The resulting table looks like this

   Name        item    date
Abel Antonio    10   01-02-12
Abel Antonio    5    01-03-12
Abel Antonio    6    01-04-12
Abel Antonio    2    01-05-12
Abel Antonio    1    01-09-12
Abel Antonio    15   01-12-12
Abel Antonio    22   01-16-12
Abel Antonio    15   01-17-12
Abel Antonio    7    01-19-12
Abel Antonio    45   02-15-12
Abel Antonio    31   02-16-12
...other names

I’d like to make the the dates as columns to make the query look like this

Name          01-02-12  01-03-12   01-04-12   01-05-12    01-09-12 ....
Abel Antonio    10         5          6          2           1     .....

Anyone know how to accomplish this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T10:00:46+00:00Added an answer on June 17, 2026 at 10:00 am

    So I think you would like to do a Pivot using a Dynamic number of columns. It depends what database you are using, but I’m guessing by the date_format statement that it is MYSQL. Here is a link to an answer that does what you want – enjoy!

    https://stackoverflow.com/a/12005676/345659

    Your code should look like this:

    $query = "
    SET @sql = NULL;
    SELECT
      GROUP_CONCAT(DISTINCT
        CONCAT('count(case when date_format(uploaddate, ''%m-%d-%y'') = ''',
          date_format(uploaddate, '%m-%d-%y'),
          ''' then 1 end) AS `',
          date_format(uploaddate, '%m-%d-%y'), '`'
        )
      ) INTO @sql
    from imsexport;
    
    SET @sql = CONCAT('SELECT uploadedby AS Name, ', 
                      @sql, 
                      ' FROM imsexport 
                        GROUP BY Name');
    
    PREPARE stmt FROM @sql;
    EXECUTE stmt;
    DEALLOCATE PREPARE stmt;";
    
    $result = mysql_query($query);
    

    I’ve used this data in the Fiddle provided by bluefeet in the linked question:

    create table imsexport
    (
      uniqueID int,
      item varchar(10),
      uploadedby varchar(50),
      uploaddate datetime
    );
    insert into imsexport values (1, 'Product A', 'Abel Antonio', '2013-01-01');
    insert into imsexport values (2, 'Product B', 'Abel Antonio', '2013-01-01');
    insert into imsexport values (3, 'Product C', 'Abel Antonio', '2013-01-01');
    insert into imsexport values (4, 'Product A', 'Abel Antonio', '2013-01-01');
    insert into imsexport values (5, 'Product A', 'Abel Antonio', '2012-01-01');
    insert into imsexport values (6, 'Product A', 'Abel Antonio', '2012-01-01');
    insert into imsexport values (7, 'Product A', 'Abel Antonio', '2012-01-01');
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: MySQL retrieve latest record for Group I have a query that groups
Possible Duplicate: mysql pivot/crosstab query I'm using MySQL and PHP, I have this table
Possible Duplicate: MySql Row Number? I have 10 names in my mysql table, I
Possible Duplicate: MySQL Orderby a number, Nulls last I have a table with columns
Possible Duplicate: php/Mysql query with inserting date fails I have a datepicker code below:
Possible Duplicate: MySQL number of items within “in clause” In mysql or postgres, is
Possible Duplicate: MySQL query that computes partial sums ok, the question is not clear
Possible Duplicate: mysql auto_increment by 5? I have a table that will have a
Possible Duplicate: MySQL query using an array Passing an array to mysql I have
Possible Duplicate: Mysql query: retrieve current date query I need a MySQL query that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.