0

I have two tables in mysql.

people is an identification table of users.. names/etc

trans is a transaction table, what the users have done (add/subtract balances)

I am wanting to return every "ident" from people, then search trans for that ident's matching rows, and then after all are returned, echo the page break, so each person gets their own page print out. (there shouldn't be so many transactions that a page is not enough..) Also to subquery the balance remaining (trans has both credits and debits, could be positive or negative).

Structure:

people.id #(not queried, unique/auto-increment)
people.name
people.location
people.ident #(assigned number that is same as trans.ident)

trans.id #(not queried, unique/auto-inc)
trans.date
trans.ident
trans.amount
trans.description

Queries:

$query = "SELECT people.ident, people.name, people.location, trans.ident, trans.date, trans.amount, trans.description FROM people LEFT JOIN trans ON people.ident = trans.ident";
$balance = mysql_result(mysql_query("SELECT SUM(amount) FROM trans WHERE ident = $ident", $dblink), 0);
$result = mysql_query($query) or die(mysql_error());

Echo results:

while($row = mysql_fetch_array($result)){
    echo $row['name']. " - ". $row['ident']. " - ". $row['amount']. " - " .row['description']; 
    echo('<div style="page-break-before: always;"></div>'); 
    }

This will print out each transaction, and they are sorted by the ident, but with div after each... I would like to print out the name and location, also their remaining balance ( once on a div.. and then trans under like so:

<div class="user">['name']  ['location'] $balance</div>  #These aren't rows, just a single return
<div class="trans">$row['amount']. " - ". $row['description'];</div>
<div style="page-break-before: always;"></div>

Thanks for any assistance!

4

1 回答 1

1

嗯,这会有点困难,因为你不能总是确定 people 表中一个人的名字/位置实际上是同一个人/位置。您的数据库未完全规范化。

话虽如此,您可以通过对查询和一些 PHP 代码进行一些小的更改来做您想做的事情 :)

 $query = "SELECT people.ident, people.name, people.location,
           trans.ident, trans.date, trans.amount, trans.description
           FROM people LEFT JOIN trans ON people.ident = trans.ident
           ORDER BY peeople.name, people.location, trans.ident"; 

回声结果:

$lastName = null;
$lastLocation = null;
while($row = mysql_fetch_array($result)){
    if (($lastName !== $row['name']) or ($lastLocation != $row['location'])) {
         echo $row['name']. " - ". $row['ident']. " - ". $row['amount']. " - " .row['description'];
         $lastName= $row['name'];
         $lastLocation = $row['location'];
    }

    /* ... output the transaction  line ... */
    echo('<div style="page-break-before: always;"></div>'); 
}
于 2013-05-28T14:30:18.227 回答