1

这是我当前的代码。

<?php
$query_production ="SELECT uploadedby, uploaddate, count(uploadedby) as items, date_format(uploaddate,'%m-%d-%Y') as date FROM imsexport WHERE uploaddate between '2013-01-01' and '2013-01-03' GROUP BY uploadedby, date ORDER BY uploadedby";
$production = mysql_query($query_production,$prepress) or die (mysql_error());
$row_production = mysql_fetch_assoc($production);
?>

<html>
<head>
<title>Untitled Document</title>
</head>
<body>
<table width="200" border="1">
   <tr>
      <td>uploadedby</td>
      <td>uploaddate</td>
      <td>items</td>
      <td>date</td>
   </tr>
 <?php do {?>
   <tr>
     <td><?php echo $row_production['uploadedby']?></td>
     <td><?php echo $row_production['uploadedby']?></td>
     <td><?php echo $row_production['items']?></td>
     <td><?php echo $row_production['date']?></td>
  </tr>

结果显示为:

 uploadedby              uploaddate           items   date
Adonis Abiera       2013-01-01 08:03:00     64  01-01-2013
Adonis Abiera       2013-01-02 05:30:00     46  01-02-2013
Aileen Alina        2013-01-02 16:29:00     15  01-02-2013
Aileen Mosende      2013-01-01 07:54:00     98  01-01-2013
Aileen Mosende      2013-01-02 06:00:00     69  01-02-2013
Ailyn Barnum        2013-01-01 09:21:00     84  01-01-2013
Ailyn Barnum        2013-01-02 09:16:00     55  01-02-2013
Alexander Dulay     2013-01-02 08:15:00     64  01-02-2013
Alfredo Santiago    2013-01-01 08:02:00     79  01-01-2013
Alfredo Santiago    2013-01-02 06:06:00     59  01-02-2013
Analyn Reyes        2013-01-01 09:04:00     65  01-01-2013
Analyn Reyes        2013-01-02 09:33:00     51  01-02-2013
Andresito Muzones   2013-01-01 08:37:00     60  01-01-2013
Andresito Muzones   2013-01-02 08:45:00     72  01-02-2013
Angelica Domingo    2013-01-01 09:28:00     68  01-01-2013
Angelica Domingo    2013-01-02 09:40:00     59  01-02-2013
Arman Romanca       2013-01-01 09:23:00     73  01-01-2013
Arman Romanca       2013-01-02 10:14:00     64  01-02-2013
Don Feliciano       2013-01-01 09:13:00     70  01-01-2013
Don Feliciano       2013-01-02 06:11:00     56  01-02-2013

我想这样显示它:

uploadedby         01-01-2013    01-02-2013
Adonis Abiera         64            46
Aileen Alina           0            15
Aileen Mosende        98            69
Ailyn Barnum          84            55
Alexander Dulay        0            64
Alfredo Santiago      79            59
Analyn Reyes          65            51
...... and so on

请注意,某些日期可能不存在某些记录。前(Aileen Alina 和 Alexander Dulay 的记录)。关于我如何做到这一点的任何想法?

4

1 回答 1

2

如果您想在 MySQL 中执行此操作,则可以使用带有表达式的聚合函数对数据进行透视:CASE

select uploadedby,
  count(case 
        when date_format(uploaddate,'%m-%d-%Y') = '01-01-2013'
        then uploadedby end) as `01-01-2013`,
  count(case 
        when date_format(uploaddate,'%m-%d-%Y') = '01-02-2013'
        then uploadedby end) as `01-02-2013`        
from imsexport
where uploaddate between '2013-01-01' and '2013-01-03'
group by uploadedby

或者,如果您不想重复,date_format()则可以使用子查询:

select uploadedby,
  count(case when date = '01-01-2013' then uploadedby end) as `01-01-2013`,
  count(case when date = '01-02-2013' then uploadedby end) as `01-02-2013`        
from
(
  select uploadedby, date_format(uploaddate,'%m-%d-%Y') date
  from imsexport
  where uploaddate between '2013-01-01' and '2013-01-03'
) src
group by uploadedby

如果您需要动态执行此操作,则可以在 MySQL 中使用准备好的语句。这将获得需要为列的日期列表:

SET @sql = NULL;
SELECT
  GROUP_CONCAT(DISTINCT
    CONCAT(
      'count(case when date = ''',
      date_format(uploaddate,'%m-%d-%Y'),
      ''' then uploadedby end) AS `',
      date_format(uploaddate,'%m-%d-%Y'), '`'
    )
  ) INTO @sql
FROM imsexport
where uploaddate between '2013-01-01' and '2013-01-03';

SET @sql = CONCAT('SELECT uploadedby, ', @sql, ' 
                  FROM
                  ( 
                    select uploadedby, date_format(uploaddate,''%m-%d-%Y'') date
                    from imsexport
                    where uploaddate between ''2013-01-01'' and ''2013-01-03''
                  ) src
                  GROUP BY uploadedby');

PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

请参阅带有演示的 SQL Fiddle

如果您担心自己可能会丢失日期,那么我建议您创建一个日历表,您可以从中获取日期列表,然后将日历表加入您的imsexport表中,以确保您拥有所有日期,即使当天没有上传。

于 2013-01-24T01:49:52.063 回答