从两个表中获取行和列

时间:2018-02-25 13:53:04

标签: php sql database sqlite

我的SQLite数据库中有两个表。 cashbank这两个表都包含以下属性(id, person_id, date, remarks, type, amount, balance) 我想获取存储在两个表中的所有事务,这些事务必须按日期排序,为此,我使用了UNION但我还希望获得cash balance和{{1}的2个单独列如果记录来自现金表,则只有bank balance应该更改且cash balance应保持相同。 这是我的代码

bank balance

This is what I'm Getting from the code above 正如你可以在输出中看到,如果源表是现金,它显示修改后的现金流,并将银行余额留空,反之亦然。 我想要的是我想在$sql = "SELECT *, 'cash' AS mop FROM cash UNION ALL SELECT *, 'bank' AS mop FROM bank ORDER BY date DESC"; $result = $db->query($sql); ?> <tr class="tab-head"> <td>Date & Time</td> <td>Narration</td> <td>Mode</td> <td>Debit</td> <td>Credit</td> <td>Cash Balance</td> <td>Bank Balance</td> </tr> <?php if(numr($result)>0){ while ($row=$result->fetchArray(SQLITE3_ASSOC)) { $date = date("d/m/Y h:i:s", strtotime($row['date'])); $remarks = $row['remarks']; $type = $row['type']; $amount = $row['amount']; $balance = $row['balance']; $mop = $row['mop']; $type == "in" ? ($credit = $amount AND $debit = "-") : ($debit = $amount AND $credit = "-"); $mop == "cash" ? ($cbal = $balance AND $bbal = "-") : ($bbal = $balance AND $cbal = "-"); ?> <tr> <td><?php echo $date; ?></td> <td><?php echo $remarks; ?></td> <td><?php echo $mop; ?></td> <td><?php echo $debit; ?></td> <td><?php echo $credit; ?></td> <td><?php echo $cbal; ?></td> <td><?php echo $bbal; ?></td> </tr> <?php } } 的位置显示未改变的余额 任何人都可以帮助我任何SQL查询或PHP代码,两者都被接受但我更喜欢一个正确的SQL查询。请帮忙

1 个答案:

答案 0 :(得分:1)

在执行$.post("http://localhost/main.php", data, response => console.log("Time saved on the server!")); 之前指定所需的列:

union all