3

次のINNERJOINクエリがあります。

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フィドルのサンプルテーブルで実際の動作を確認できます

私の目的は、以下を返すことです。

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 @ &pound;".$amount." each<br><br>";
           echo date("jS F Y",strtotime($date))."<br><br>";
           $total_work=$total_work+$total_group;
        }
        else {
           echo $count." ".$work." @ &pound;".$amount."<br><br>";
           echo date("jS F Y",strtotime($date))."<br><br>";
           $total_work=$total_work+$total_group;
        }

        }

グループ化ではなく、行ごとに1行を取得しています。

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
4

1 に答える 1

1

問題は、各行の「頭」を呼び出しているという事実にあるようです。したがって、すでに呼び出されているかどうかを最初に確認する必要があります。以下が役立つことを願っています:

$stmt->bind_param("s", $name1);
$stmt->execute();
$stmt->store_result();  
$stmt->bind_result($work,$amount,$count,$total_group,$date);

$last_work = "";
while ($stmt->fetch()) {
    if($work != $last_work || $amount != $last_amount){
        if ($count>1) {
           echo "<br>".$count." ".$work."s @ &pound;".$amount." each<br><br>";

        }
        else {
           echo "<br>".$count." ".$work." @ &pound;".$amount."<br><br>";
        }
        $last_work = $work;
        $last_amount = $amount;
    }
    echo date("jS F Y",strtotime($date))."<br>";
    $total_work=$total_work+$total_group;
}

どちらの場合も同じように呼び出されていたので、 echo dateand を外側に移動しました( and )$total_work$count >1else

于 2013-02-22T13:01:23.717 に答える