从mysql到html表中统计每月每天的记录
我想把所有mySql结果放在一个html表中。这是mySql:从mysql到html表中统计每月每天的记录,html,mysql,count,html-table,record,Html,Mysql,Count,Html Table,Record,我想把所有mySql结果放在一个html表中。这是mySql: SELECT date(vwr_date) AS mon, date(vwr_date) AS date, count(vwr_cid) AS views FROM car_viewer WHERE Year(vwr_date)='2012' AND vwr_tid='18' GROUP BY date ORDER BY date DESC 根据上面的查询,结果如下: date views 2012-10-2
SELECT date(vwr_date) AS mon, date(vwr_date) AS date, count(vwr_cid) AS views
FROM car_viewer
WHERE Year(vwr_date)='2012' AND vwr_tid='18'
GROUP BY date
ORDER BY date DESC
根据上面的查询,结果如下:
date views
2012-10-23 14
2012-10-22 339
2012-10-21 305
2012-10-20 354
....
2012-10-01 291
2012-09-30 246
2012-09-29 297
...
2012-09-01 281
<table border="0" cellpadding="3" cellspacing="1" width="100%">
<thead>
<tr>
<th>Mon Year / Date</th>
<?
for($i=1;$i<32;$i++){
echo "<th>$i</th>";
}
?>
</tr>
</thead>
<tbody>
<?
$_thisYear=date("Y");
$sql_nViewer="select date(vwr_date) as mon, date(vwr_date) as date, count(vwr_cid) as views from car_viewer where Year(vwr_date)='$_thisYear' and vwr_tid='$_SESSION[admin_id]' GROUP BY date ORDER BY date desc";
echo $sql_nViewer;
$result_nViewer=mysql_db_query($dbname,$sql_nViewer);
$rec_nViewer=mysql_fetch_array($result_nViewer);
$_monYear=date("M Y",strtotime($rec_nViewer[mon]));
$_monYear2=date("Y-n",strtotime($rec_nViewer[mon]));
echo "<tr><td>$_monYear</td>";//month name
$day=1;
while($rec_nViewer=mysql_fetch_assoc($result_nViewer)){
$_nViewerDBdate=$rec_nViewer['date'];
$_nViewerDBviews=$rec_nViewer['views'];
echo "
<td>$_nViewerDBviews</td>
";$day++;
if($day==$_daysInMon+1){
echo "</tr>";
}
}
?>
</tbody>
</table>
mysql的结果非常好。现在这是一个问题。我尝试了很多次,并根据car_viewer.vwr_date中的每个日期,以不同的方式将这些视图放入。所以我这样写:
date views
2012-10-23 14
2012-10-22 339
2012-10-21 305
2012-10-20 354
....
2012-10-01 291
2012-09-30 246
2012-09-29 297
...
2012-09-01 281
<table border="0" cellpadding="3" cellspacing="1" width="100%">
<thead>
<tr>
<th>Mon Year / Date</th>
<?
for($i=1;$i<32;$i++){
echo "<th>$i</th>";
}
?>
</tr>
</thead>
<tbody>
<?
$_thisYear=date("Y");
$sql_nViewer="select date(vwr_date) as mon, date(vwr_date) as date, count(vwr_cid) as views from car_viewer where Year(vwr_date)='$_thisYear' and vwr_tid='$_SESSION[admin_id]' GROUP BY date ORDER BY date desc";
echo $sql_nViewer;
$result_nViewer=mysql_db_query($dbname,$sql_nViewer);
$rec_nViewer=mysql_fetch_array($result_nViewer);
$_monYear=date("M Y",strtotime($rec_nViewer[mon]));
$_monYear2=date("Y-n",strtotime($rec_nViewer[mon]));
echo "<tr><td>$_monYear</td>";//month name
$day=1;
while($rec_nViewer=mysql_fetch_assoc($result_nViewer)){
$_nViewerDBdate=$rec_nViewer['date'];
$_nViewerDBviews=$rec_nViewer['views'];
echo "
<td>$_nViewerDBviews</td>
";$day++;
if($day==$_daysInMon+1){
echo "</tr>";
}
}
?>
</tbody>
</table>
星期一/年/日
上面代码的结果很奇怪-不是我想要的。我需要这样显示数据:
因此,请建议我应该做什么,使它如我所期望的。关于您可以从mysql返回该格式的数据
SELECT MONTH_v, YEAR_V
GROUP_CONCAT(IF(day_v=1, views, null)) AS '1',
GROUP_CONCAT(IF(day_v=2, views, null)) AS '2',
----
GROUP_CONCAT(IF(day_v=31, views, null)) AS '31'
FROM
(
SELECT DAY(vwr_date) AS day_v,
MONTH(vwr_date) AS MONTH_v,
Year(vwr_date) AS YEAR_V,
date(vwr_date) AS date_v,
count(vwr_cid) AS views
FROM car_viewer
WHERE Year(vwr_date)='2012' AND vwr_tid='18'
GROUP BY date_v
)
GROUP BY MONTH_v, YEAR_V
ORDER BY MONTH_v, YEAR_V DESC
我有一个错误:
#1064-您的SQL语法有一个错误;查看与MySQL服务器版本对应的手册,了解使用接近'GROUP_CONCAT(IF(day_v=1,views,null))作为'1',GROUP_CONCAT(IF(day_v=2,views,n'在第2行
的正确语法,然后我认为缺少或超过逗号可能会有问题:\1248-每个派生表都必须有自己的别名