【问题标题】:Passing sql variables from INNER JOIN SQL query to PHP script将 SQL 变量从 INNER JOIN SQL 查询传递到 PHP 脚本
【发布时间】:2013-02-08 01:47:58
【问题描述】:

我有以下 INNER JOIN 查询:

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 fiddle 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 @ &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;
        }

        }

我每行得到一行,而不是分组,即:

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

【问题讨论】:

    标签: php sql inner-join


    【解决方案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 date$total_work 移到了外面,因为它们在两种情况下都被同等调用($count &gt;1else

    【讨论】:

    • 谢谢。这似乎是正确的,但输出仍然不太正确 - 我只返回一个分组,因为 $last_work 变量停止添加任何进一步的分组。
    • 好吧,如果它不一样,它应该添加一个新的“头”。我可以看到输出以对解决方案进行新的尝试吗? :)
    • 我已将当前输出添加到问题的末尾。
    • 好的,我解决了这个问题。就是在上面的“治疗会议”的情况下,有时工作是相同的,但数量不同,所以我需要添加另一个变量$last_amount,然后也检查这个变量,即if($work != $last_work OR $amount != $last_amount)。如果您想修改答案,我可以将其标记为正确。谢谢
    • 我修改了答案,但我仍然不确定它是否正确。我会继续考虑 :) 顺便说一句,我更改了间距,因此输出与您正在寻找的相似。
    猜你喜欢
    • 2019-02-16
    • 1970-01-01
    • 2021-10-25
    • 2010-12-27
    • 2015-01-10
    • 1970-01-01
    • 1970-01-01
    • 2010-12-19
    • 2012-11-04
    相关资源
    最近更新 更多