创建内部联接以创建正确的输出

时间:2014-05-03 03:47:01

标签: php mysql sql

我试图让我的数据看起来像下面的输出。我有一个内部加入的SQL  连接两个表,以便它们输出数据,如下所示。我不知道如何做到这一点可以有人给我一个例子

这就是输出现在的样子

First American
2014-03-17 Blackwell Vancouver 115
First American
2014-04-21 Kurth Vancouver 85
Title Source
2014-01-04 Kurth Vancouver 85
Title Source
2014-03-11 Kurth Vancouver 85
Title Source
2014-03-17 Kurth Vancouver 85

这就是我想要的样子

First American 

2014-03-17 Blackwell Vancouver 115
2014-04-21 Kurth Vancouver 85

Title Source

2014-01-04 Kurth Vancouver 85
2014-03-11 Kurth Vancouver 85
2014-03-17 Kurth Vancouver 85



$result = $db->query("SELECT customers.name,signings.pid,signings.done,signings.signstart,
signings.lname,signings.ordern,  signings.city,signings.samount,signings.prtamount,
signings.faxamount FROM customers Inner Join signings On signings.custid = customers.custid 
WHERE signings.pid = 6 AND signings.done = 1 GROUP BY customers.name, signings.pid, 
signings.done,signings.signstart");
foreach($result as $key => $income) {

  echo $income['name'];  

  echo '<br>';
  $total = $income['samount'] + $income['prtamount'] + $income['faxamount'];
  echo $income['signstart'] . ' ' .$income['lname']. ' ' .$income['city'] . ' ' .$total;

  echo '<br>';
}    

1 个答案:

答案 0 :(得分:2)

您可以像这样修改PHP:

$current=null;
foreach($result as $key => $income) {
if($current!=$income['name']){
    echo $income['name'];
    $current = $income['name'];  
}

//other stuff
}

希望这能解决它!