質問

I am trying to generate a weekly report from selecting year and week. below is my code. i work until i add the WHERE condition.

<form action="" method="POST">
 Select Year:<select name="sortyear" id="sortyear">
                 <option value="2012">2012</option>
                 <option value="2013">2013</option>
                 <option value="2014">2014</option>
                 <option value="2015">2015</option>
             </select>
 Select Week:<select name="week" id="week">
          ...</select>

<?php $sql = mysql_query("SELECT SUM(credit) AS credit, depot, Date_Format(`timestamp`,'%U%Y') AS period FROM transactions where Date_Format(`timestamp`,'%U%Y')='102013' group by Date_Format(`timestamp`,'%U%Y') DESC"); while($row11 = mysql_fetch_array( $sql )) 
   {
       $credit = $row11['credit'].'<br>';
       $depot = $row11['depot'].'<br>';
       $period = $row11['period'].'<br>';
   }

$sortyear = $_REQUEST['sortyear'];
$week = $_REQUEST['week'];?>     

Please Help.

役に立ちましたか?

解決

Your query is going to be hideously inefficient. You're taking a mysql date/time field, converting it to a string, then using that string for comparisons. That precludes any use of indexes.

Why not simply

SELECT ...
...
WHERE (YEAR(timestamp) = 2013) AND (MONTH(timestamp) = 10)
GROUP BY YEAR(timestamp), MONTH(timestamp)

?

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top