我已经为此苦苦挣扎了一段时间了。我不确定 MYSQL 查询是执行此操作的最佳方法,但我不知所措。
我在某些日期为某些人提供了条目,但是它们不会总是在同一日期。例如:
Person Date Product
Mike 2012/12/1 Pants
Chris 2012/12/4 Shoes
Mike 2012/12/4 Pants
Julian 2012/12/5 Hat
我使用此表来分析趋势报告,我需要选择一个日期范围,例如 2012/12/2 - 2012/12/10。我需要一张表格来显示每个人每天的穿着,这样我就可以看到任何一天穿了多少百分比。输出表如下所示:
Person Date Product
Mike 2012/12/1 Pants
Mike 2012/12/2 Pants
Mike 2012/12/3 Pants
Chris 2012/12/4 Shoes
Mike 2012/12/4 Shoes
Julian 2012/12/5 Hat
Mike 2012/12/5 Shoes
Chris 2012/12/5 Shoes
Julian 2012/12/6 Hat
Mike 2012/12/6 Shoes
Chris 2012/12/6 Shoes
Julian 2012/12/7 Hat
Mike 2012/12/7 Shoes
Chris 2012/12/7 Shoes
等等....取决于日期范围。理想情况下,这样的表格会更好:
Date Product Worn Total
2012/12/1 Pants 1 1
2012/12/1 Shoes 0 1
2012/12/1 Hat 0 1
2012/12/2 Pants 1 1
2012/12/2 Shoes 0 1
2012/12/2 Hat 0 1
2012/12/3 Pants 1 1
2012/12/3 Shoes 0 1
2012/12/3 Hat 0 1
2012/12/4 Pants 0 2
2012/12/4 Shoes 2 2
2012/12/4 Hat 0 2
2012/12/4 Pants 0 3
2012/12/4 Shoes 2 3
2012/12/4 Hat 1 3
我目前正在使用MySql DB在PHP中工作。任何想法,建议,不胜感激。
伪代码主要是:
for(EACH day in date range){
$query = SELECT COUNT(product) from table WHERE date="day" AND product = "hat";
$getResults=mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($getResults))
{
$inventory_count = $row['COUNT(product)'];
}
echo 'Date'. $date.' - 'Product: hat - total = '. $inventory_count;
$query = SELECT COUNT(product) from table WHERE date="day" AND product = "pants";
$getResults=mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($getResults))
{
$inventory_count = $row['COUNT(product)'];
}
echo 'Date'. $date.' - 'Product: pants' - 'total = '. $inventory_count;
$query = SELECT COUNT(product) from table WHERE date="day" AND product = "shoes";
$getResults=mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($getResults))
{
$inventory_count = $row['COUNT(product)'];
}
echo 'Date'. $date.' - 'Product: shoes' - 'total = '. $inventory_count;
}
这可能不是获取它的最有效方法,但它应该为您提供所需的数据。放入一些TR和TD,将其包装在表格中,您应该拥有所需的内容。
如果日期是 mysql 日期字段,则第一个表就是
SELECT * FROM table WHERE Date > start_range AND Date < end_range
其中start_range和end_range是您想要的日期范围,格式为 YYYY/MM/DD。