3
我有以下INNER JOIN查詢:傳遞SQL變量連接SQL查詢到PHP腳本
SELECT b.*, c.date2
FROM (
SELECT a.work, a.amount,
COUNT(*) totalCount,
SUM(Amount) totalAmount
FROM work_times a WHERE Organisation=?
GROUP BY a.work, a.amount
) b
INNER JOIN
(
SELECT a.work, a.amount, DATE_FORMAT(Date,'%D %M %Y') date2,
date
FROM work_times a
) c ON b.work = c.work and b.amount=c.amount
ORDER BY b.work, b.totalCount, c.date
你可以看到它在行動上一個示例表上的SQL小提琴here。
我的目標是返回如下:
5 consultancy sessions @ £50 each: £250
1st February 2013
8th February 2013
15th February 2013
22nd February 2013
1st March 2013
3 therapy sessions @ £40 each: £120
2nd February 2013
9th February 2013
16th February 2013
2 therapy sessions @ £20 each: £40
3rd February 2013
10th February 2013
但是,使用下面的PHP:
$stmt->bind_param("s", $name1);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($work,$amount,$count,$total_group,$date);
while ($stmt->fetch()) {
if ($count>1) {
echo $count." ".$work."s @ £".$amount." each<br><br>";
echo date("jS F Y",strtotime($date))."<br><br>";
$total_work=$total_work+$total_group;
}
else {
echo $count." ".$work." @ £".$amount."<br><br>";
echo date("jS F Y",strtotime($date))."<br><br>";
$total_work=$total_work+$total_group;
}
}
我收到一條線的每一行,而不是分組,即:
5 Consultancy Sessions @ £50.00
1st February 2013
5 Consultancy Sessions @ £50.00
8th February 2013
5 Consultancy Sessions @ £50.00
15th February 2013
...etc
而我不知道如何修改我的PHP來獲得所需的輸出。
電流輸出
5 Consultancy Sessions @ £50.00
1st February 2013
8th February 2013
15th February 2013
22nd February 2013
1st March 2013
2nd February 2013
9th February 2013
16th February 2013
3rd February 2013
10th February 2013
感謝同樣調用。這似乎是沿着正確的路線,但輸出仍然不是很正確 - 我只得到一個分組返回$ last_work變量停止任何進一步的分組被添加。 – Nick 2013-02-22 13:11:11
那麼,在不一樣的情況下,它應該添加一個新的「頭」。我能否看到輸出對解決方案有新的嘗試? :) – Sikian 2013-02-22 13:18:07
我已經添加了當前的輸出到我的問題的結尾。 – Nick 2013-02-22 13:23:28