我已经在这个问题上挣扎了一段时间了。我不确定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中工作。任何想法,建议,我们都很感激。
发布于 2012-05-18 04:20:27
伪代码主要是:
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,把它包在一个表格里,你就应该得到你想要的东西了。
发布于 2012-05-18 04:04:41
如果Date是mysql日期字段,那么第一个表就是
SELECT * FROM table WHERE Date > start_range AND Date < end_range其中,start_range和end_range是所需的日期范围,格式为YYYY/MM/DD。
https://stackoverflow.com/questions/10641852
复制相似问题