0

早上好,我有下面的 mysql 查询来显示销售情况良好的年份,直到年份发生变化,因为它现在显示 2012 年 1 月和 2013 年 1 月的组合。

为了解决这个问题,我放置了一个下拉表单框 (id=YearSelect),其值为 2012 和 2013,是否可以使用所选值控制此查询?如果最终用户选择 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

改变这个怎么样WHERE

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

到:

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

那应该够了……

于 2013-01-07T11:33:41.187 回答
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 回答