PHP / MySQL - 组合选择查询

时间:2013-01-30 05:28:27

标签: php mysql

我有3个MySQL查询,就像这样;

$q1 = "SELECT id,date,quantity1,quantity2,total FROM test WHERE type='pencil' AND MONTH(date)='$month' AND YEAR(date)='$year' ORDER BY id";
$r1 = mysql_query($q1);

$q2 = "SELECT SUM(total) AS stotal FROM test WHERE type='pencil' AND MONTH(date)='$month' AND YEAR(date)='$year' GROUP BY id ORDER BY id";
$r2 = mysql_query($q2);

$q2 = "SELECT AVG(ROUND(total)) AS atotal FROM test WHERE type='pencil' AND MONTH(date)='$month' AND YEAR(date)='$year' GROUP BY id ORDER BY id";
$r2 = mysql_query($q2);

我如何结合这3个查询?所以我可以在一个循环中使用它。我在while循环中尝试了多个语句,就像这样;

$lastr = '';
while ((list($id, $date, $q1, $q2, $total) = mysql_fetch_row($r1)) && ($row =  mysql_fetch_array($r2)) && ($row =  mysql_fetch_array($r3)))  {
if ($lastr != $id ) {
    if ($lastr != '') {
        // output row
    echo "<tr><th>$lastr</th><th>$stotal</th><th>$atotal</th><td>";
        echo join('</td><td>', $rowdata) . "</td></tr>\n";
    }
    $rowdata = $empty;
    $lastr = $id;
}
$rowdata[$rdate]  = $q1 .= '</td><td>' . $q2 .= '</td><td>' . $total;
}

当我只使用第一个查询时,所有内容都打印在我的桌面上,但是当我添加另外两个“和”语句时,输出受到限制。 顺便说一下,我试图生成一个动态列和行,只有在我使用while循环中的第一个查询时才有效。感谢


非常感谢:raheel shan

我的问题的答案是:

SELECT
  t.id,
  t.date,
  t.quantity1,
  t.quantity2,
  t.total,
  lt.stotal,
  lt.atotal
FROM test as t
  left join (select
           id,
           SUM(total)   AS stotal,
           AVG(ROUND(total)) AS atotal
         from test
         WHERE type = 'pencil'
         AND MONTH(date) = '$month'
         AND YEAR(date) = '$year'
         GROUP BY id) as lt
    on lt.id = t.id
WHERE type = 'pencil'
    AND MONTH(date) = '$month'
    AND YEAR(date) = '$year'
ORDER BY id

4 个答案:

答案 0 :(得分:0)

U可以这样组合............

SELECT id,date,quantity1,quantity2,total,(SELECT SUM(total)  FROM test WHERE type='pencil' AND MONTH(date)='$month' AND YEAR(date)='$year' GROUP BY id ORDER BY id) as sum_total,(SELECT AVG(ROUND(total))  FROM test WHERE type='pencil' AND MONTH(date)='$month' AND YEAR(date)='$year' GROUP BY id ORDER BY id") as avg_total FROM test WHERE type='pencil' AND MONTH(date)='$month' AND YEAR(date)='$year' ORDER BY id";

答案 1 :(得分:0)

您可以像这样添加查询

$q1 = "SELECT id,date,quantity1,quantity2,total,AVG(ROUND(total)) AS stotal, AVG(ROUND(total)) AS atotal FROM test WHERE type='pencil' AND MONTH(date)='$month' AND YEAR(date)='$year' GROUP BY  id,date,quantity1,quantity2,total,stotal,atotal ORDER BY id";
$r1 = mysql_query($q1);

你需要group by子句的原因是因为你不能在group by语句之外选择非aggragate是select中存在的aggragate函数。

希望这有帮助。

答案 2 :(得分:0)

$q1 = "SELECT id,date,quantity1,quantity2,total,SUM(total) AS stotal,AVG(ROUND(total)) AS atotal FROM test WHERE type='pencil' AND MONTH(date)='$month' AND YEAR(date)='$year' ORDER BY id";
$r1 = mysql_query($q1);

答案 3 :(得分:0)

您可以使用此单个派生查询

SELECT
  t.id,
  t.date,
  t.quantity1,
  t.quantity2,
  t.total,
  lt.stotal,
  lt.atotal
FROM test as t
  left join (select
           id,
           SUM(total)   AS stotal,
           AVG(ROUND(total)) AS atotal
         from test
         WHERE type = 'pencil'
         AND MONTH(date) = '$month'
         AND YEAR(date) = '$year'
         GROUP BY id) as lt
    on lt.id = t.id
WHERE type = 'pencil'
    AND MONTH(date) = '$month'
    AND YEAR(date) = '$year'
ORDER BY id

或者这更简单

SELECT
  t.id,
  t.date,
  t.quantity1,
  t.quantity2,
  t.total,
  SUM(lt.stotal) AS stotal,
  AVG(ROUND(lt.atotal)) AS atotal
FROM test as t
  left join test as lt
    on lt.id = t.id
WHERE type = 'pencil'
    AND MONTH(date) = '$month'
    AND YEAR(date) = '$year'
ORDER BY id