0

おはようございます。2012 年 1 月と 2013 年 1 月を合わせて表示するように、年が変更されるまで正常に機能していた年の売上を表示するための mysql クエリを以下に示します。

解決を試みるために、2012 と 2013 の値を持つドロップダウン フォーム ボックス (id=YearSelect) を配置しました。選択した値でこのクエリを制御できますか? エンド ユーザーが 2012 を選択した場合、その値は where 句のタイプとして列 Year に送信されますか?

<form id="yearselect">
<select name="year">
  <option value="">Select year </option>
  <option value="2012">2012</option>
  <option value="2013">2013</option>
</select>
</form>


Select
  *,
  If(q.Adviser Is Null, 1, 0) As remove
From
  (Select
      a.ContactFullName As Adviser,  
      YEAR(b.CaseDate) As Year,
      Sum(If(Month(b.StatusSubmittedDate) = 1, b.CaseCommission, 0)) As Jan,
      Sum(If(Month(b.StatusSubmittedDate) = 2, b.CaseCommission, 0)) As Feb,
      Sum(If(Month(b.StatusSubmittedDate) = 3, b.CaseCommission, 0)) As Mar,
      Sum(If(Month(b.StatusSubmittedDate) = 4, b.CaseCommission, 0)) As Apr,
      Sum(If(Month(b.StatusSubmittedDate) = 5, b.CaseCommission, 0)) As May,
      Sum(If(Month(b.StatusSubmittedDate) = 6, b.CaseCommission, 0)) As Jun,
      Sum(If(Month(b.StatusSubmittedDate) = 7, b.CaseCommission, 0)) As Jul,
      Sum(If(Month(b.StatusSubmittedDate) = 8, b.CaseCommission, 0)) As Aug,
      Sum(If(Month(b.StatusSubmittedDate) = 9, b.CaseCommission, 0)) As Sep,
      Sum(If(Month(b.StatusSubmittedDate) = 10, b.CaseCommission, 0)) As Oct,
      Sum(If(Month(b.StatusSubmittedDate) = 11, b.CaseCommission, 0)) As Nov,
      Sum(If(Month(b.StatusSubmittedDate) = 12, b.CaseCommission, 0)) As Decb,
      Sum(b.CaseCommission) As Total
    From
      tblcontacts a Inner Join
      tblcases b On a.ContactID = b.ContactAssignedTo
    Where
      WHERE Year(b.StatusSubmittedDate) = ".(int)$_POST['year']."
    Group By
      a.ContactFullName WITH ROLLUP
    Having
      Sum(b.CaseCommission) > 0.01) q
Order By
  If(q.Adviser Is Null, 1, 0), q.Total Desc

アドバイスをいただければ幸いです。

4

2 に答える 2

1

PHPタグを考慮すると、PHPベースページに結果を表示していると思います。次のコードを使用して、必要な処理を実行できます。

<form id="yearselect" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" >
<select name='selyear' onchange='document.getElementById("yearselect").submit()'>
  <option value=''> - Select year </option>
  <option value='2012'>2012</option>
  <option value='2013'>2013</option>
</select>
</form>

<?
$y = date("Y");
if (isset($_POST['selyear']))
{
  if (checkdate(1, 1, $_POST['selyear']) && $_POST['selyear']>=2012)
    $y = $_POST['selyear'];
}

$query = 'Select
  *,
  If(q.Adviser Is Null, 1, 0) As remove
From
  (Select
      a.ContactFullName As Adviser,  
      YEAR(b.CaseDate) As Year,
      Sum(If(Month(b.StatusSubmittedDate) = 1, b.CaseCommission, 0)) As Jan,
      Sum(If(Month(b.StatusSubmittedDate) = 2, b.CaseCommission, 0)) As Feb,
      Sum(If(Month(b.StatusSubmittedDate) = 3, b.CaseCommission, 0)) As Mar,
      Sum(If(Month(b.StatusSubmittedDate) = 4, b.CaseCommission, 0)) As Apr,
      Sum(If(Month(b.StatusSubmittedDate) = 5, b.CaseCommission, 0)) As May,
      Sum(If(Month(b.StatusSubmittedDate) = 6, b.CaseCommission, 0)) As Jun,
      Sum(If(Month(b.StatusSubmittedDate) = 7, b.CaseCommission, 0)) As Jul,
      Sum(If(Month(b.StatusSubmittedDate) = 8, b.CaseCommission, 0)) As Aug,
      Sum(If(Month(b.StatusSubmittedDate) = 9, b.CaseCommission, 0)) As Sep,
      Sum(If(Month(b.StatusSubmittedDate) = 10, b.CaseCommission, 0)) As Oct,
      Sum(If(Month(b.StatusSubmittedDate) = 11, b.CaseCommission, 0)) As Nov,
      Sum(If(Month(b.StatusSubmittedDate) = 12, b.CaseCommission, 0)) As Decb,
      Sum(b.CaseCommission) As Total
    From
      tblcontacts a Inner Join
      tblcases b On a.ContactID = b.ContactAssignedTo
    Where
      b.StatusSubmittedDate > \''.$y.' - 01 - 01\'
      AND b.StatusSubmittedDate < \''.$y.' - 12 - 31\'
    Group By
      a.ContactFullName WITH ROLLUP
    Having
      Sum(b.CaseCommission) > 0.01) q
Order By
  If(q.Adviser Is Null, 1, 0), q.Total Desc'


  //output results
?>

あなたがそれが機能すると言ったので、今私はあなたのクエリをチェックしませんでした。また、選択リストの変更時にフォームを投稿する代わりに、AJAXなどを使用してフォームを変更して改善することもできます。しかし、私はあなたがその考えを理解していると思います。

また、2012年や2013年だけでなく、有効な年を確認するようになりました。これは、必要な年によって異なります。

于 2013-01-07T11:52:57.567 に答える
1

これを変更するのはどうですかWHERE

Where
  b.StatusSubmittedDate > '2012 - 01 - 01'

に:

Where
  YEAR(b.StatusSubmittedDate) = ".(int)$YEAR_VALUE_FROM_DROPDOWN."

それで十分なはずです...

于 2013-01-07T11:33:41.187 に答える