我在mysql中有两个表
people是用户的识别表。名称/等
trans是一个事务表,用户做了什么(添加/减去余额)
我想从人们返回每个"标识",然后搜索该标识的匹配行,然后在所有返回后,返回换页符,因此每个人都得到自己的页面打印出来。(不应该有太多的交易,一个页面是不够的…)还要子查询剩余余额(trans有贷方和借方,可以是正的,也可以是负的)。
结构:
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
查询:
$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());
回声结果:
while($row = mysql_fetch_array($result)){
echo $row['name']. " - ". $row['ident']. " - ". $row['amount']. " - " .row['description'];
echo('<div style="page-break-before: always;"></div>');
}
这将打印出每个事务,它们按标识进行排序,但每个后面都有div…我想打印出名称和位置,也是他们的剩余余额(一旦在一个div..然后在下面像这样转换:
<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>
谢谢你的帮助!
这将是一个有点困难因为你不能总是确保人名表中一个人的名字/位置实际上是同一个人/位置。您的数据库没有完全规范化。
话虽如此,您可以通过对查询和一些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>');
}