如何将两个SQL查询合并为一个并在foreach循环中显示PHP

5t7ly7z5  于 2023-09-29  发布在  PHP
关注(0)|答案(2)|浏览(136)

我试图在一个表中显示两个特定的人(一对夫妇的收入)(通过foreach循环PHP)。每个人都应该显示在一列和每行的收入。
为了连接夫妇/人,我创建了一个表j_b_controller,它可以通过joint_budget_id将夫妇连接在一起。j_b_controller还有一个position_order列,用于控制person 1和person 2的位置。Person 1的position_order = 1,Person 2 = 2。
当其中一个帐户登录时,我启动此会话

$_SESSION['budget_id'] = $jointbudget['joint_budget_id'];

一个例子:收入表看起来像这样

  • person 1有income 1,income 2,income 3和income 5.
  • 人2有收入1
  • 每个人的收入1是不一样的。
  • 每个收入都有自己特定的row_type_id,所以income1row_type_id = 1

Income table
| ID|名称|帐户ID|量|注意|行类型标识|添加日期|
| --|--|--|--|--|--|--|
| 1 |收入1| 1 |14000.99|| 1 |2019 -01- 22 00:00:00|
| 2 |收入2| 1 |1900.99|| 2 |2019 -01- 23 00:00:00|
| 3 |收入3| 1 |3269.99|| 3 |2019 -01- 16 00:00:00|
| 4 |收入1| 2 |5200.99|| 1 |2019 -01- 22 00:00:00|
| 5 |收入5| 1 |一百三十块九毛九|| 5 |2019 -01- 22 00:00:00|
j_B_controller表如下所示

  • Person 1具有joint_budget_id = 1和position_order = 1
  • Person 2具有joint_budget_id = 1和position_order = 2

j_b_controller table
| ID|帐户ID|联合预算ID|接受邀请|位置顺序|添加日期|
| --|--|--|--|--|--|
| 1 | 1 | 1 || 1 |2022-02-10 11:45:22|
| 2 | 2 | 1 || 2 |2022-02-11 11:45:24|
所有内容都应该显示在HTML表中(PHP foreach循环)
ORDER BY row_type_id
| 人1|人2|
| --|--|
| 收入1|收入1|
| 收入2|空|
| 收入3|空|
| 收入5|空|
希望你能看到下面的图片x1c 0d1x
应该连接/联合的两个SQL查询是

//Select incomes from person with position_order = 1
    $stmt = $pdo->prepare('SELECT i.id, i.name, i.amount AS p2_amount, i.row_type_id FROM incomes i JOIN j_b_controller jb ON i.account_id = jb.account_id AND jb.joint_budget_id = ? AND jb.position_order = **1** ORDER BY i.id AND i.row_type_id ASC');
    $stmt->execute([$_SESSION['budget_id']]);
    $income_rows_122s = $stmt->fetchAll(PDO::FETCH_ASSOC);

//Select incomes from person with position_order = 2
    $stmt = $pdo->prepare('SELECT i.id, i.name, i.amount AS p2_amount, i.row_type_id FROM incomes i JOIN j_b_controller jb ON i.account_id = jb.account_id AND jb.joint_budget_id = ? AND jb.position_order = **2** ORDER BY i.id AND i.row_type_id ASC');
    $stmt->execute([$_SESSION['budget_id']]);
    $income_rows_122s = $stmt->fetchAll(PDO::FETCH_ASSOC);

我已经尝试使用UNION这两个,我已经尝试做一个子查询
我的子查询是目前为止我得到的关闭的,但是对于子查询,我只能为person 2返回1行。并且它重复与Person 1具有收入的次数一样多的次数。

$stmt = $pdo->prepare('SELECT i.*, i.amount AS p1_amount, (SELECT amount FROM incomes i JOIN j_b_controller jb ON i.account_id = jb.account_id AND jb.joint_budget_id = ? AND jb.position_order = 2 ORDER BY i.id AND i.row_type_id ASC) AS p2_amount FROM incomes i JOIN  j_b_controller jb ON i.account_id = jb.account_id AND jb.joint_budget_id = ? AND jb.position_order = 1 ORDER BY i.id AND i.row_type_id ASC');
    $stmt->execute([$_SESSION['budget_id'], $_SESSION['budget_id']]);
    $income_rows_1s = $stmt->fetchAll(PDO::FETCH_ASSOC);

我的foreach看起来像这样

<tbody>
                <?php if (empty($income_rows_1s)) : ?>
                    <tr>
                        <td colspan="8" style="text-align:center;">There are no objects</td>
                    </tr>
                <?php else : ?>
                    <?php foreach ($income_rows_1s as $income_rows_1) : ?>
                       
                        <?php if (empty($income_rows_1['row_type_id'])) :?>

                        <?php else : ?>
                            <tr role="row" class="item" data-modal=".incomeInfo<?= md5($income_rows_1['row_type_id']) ?>">
                                <td role="gridcell" class="table-edit-link"></td>
                                <td role="gridcell" class="table-view-link"><a href="#"><?= $income_rows_1['name'] ?></a></td>
                                <?php if (empty($income_rows_1['p1_amount']) or ($income_rows_1['p1_amount']) == 0.00) :  ?>
                                    <td role="gridcell"></td>
                                <?php else : ?>
                                <td role="gridcell"><?= number_format($income_rows_1['p1_amount'], $decimals, ',', '.'); ?></td>
                                <?php endif; ?>
                                <?php if (empty($income_rows_1['row_type_id']) or (empty($income_rows_1['p2_amount']) or ($income_rows_1['p2_amount']) == 0.00)) : ?>
                                    <td role="gridcell"></td>
                                 <?php else : ?>
                                <td role="gridcell"><?= number_format($income_rows_1['p2_amount'], $decimals, ',', '.'); ?></td>
                                <?php endif; ?>
                                <td role="gridcell"><?= number_format(($income_rows_1['p1_amount'] + $income_rows_1['p2_amount']), $decimals, ',', '.') ?></td>
                                <td role="gridcell"><?= number_format(($income_rows_1['p1_amount'] + $income_rows_1['p2_amount']) * 12, $decimals, ',', '.') ?></td>
                            </tr>
                        <?php endif; ?>
                    <?php endforeach; ?>
                <?php endif; ?>
            </tbody>

结果如下(图片)

Person 2应该只显示income 1一次,其余为空。如果person 2有income 6,那么person 1的income 6行将为空,然后person 2在表中有income 6金额。
希望这是有意义的。我不是一个熟练/受过教育的程序员,但我喜欢在业余时间编程一点。

dfddblmv

dfddblmv1#

这将在一个查询中为您提供所需的结果集:

SELECT
    IFNULL(i.row_type_id, 'Total') AS Name,
    SUM(IF(jb.position_order = 1, i.amount, NULL)) AS "Person 1",
    SUM(IF(jb.position_order = 2, i.amount, NULL)) AS "Person 2"
FROM incomes i
INNER JOIN j_b_controller jb ON i.account_id = jb.account_id
WHERE jb.joint_budget_id = 1
GROUP BY i.row_type_id WITH ROLLUP
ORDER BY ISNULL(i.row_type_id), i.row_type_id

结果:

+---------+----------+----------+
| Name    | Person 1 | Person 2 |
+---------+----------+----------+
| 1       | 14000.99 |  5200.99 |
| 2       |  1900.99 |     NULL |
| 3       |  3269.99 |     NULL |
| 5       |   130.99 |     NULL |
| Total   | 19302.96 |  5200.99 |
+---------+----------+----------+

在PHP代码中,可以在迭代表时检查NULL值。

owfi6suc

owfi6suc2#

如果我没理解错的话,你在找这样的东西?

SELECT i.amount AS p1_amount, ii.amount AS p2_amount FROM incomes i 
JOIN j_b_controller jb ON i.account_id = jb.account_id AND jb.joint_budget_id = ? AND jb.position_order = **1** 
LEFT JOIN incomes ii ON ii.account_id = jb.account_id AND jb.joint_budget_id = ? AND jb.position_order = **2** 
ORDER BY i.id AND i.row_type_id ASC, ii.id AND ii.row_type_id ASC;

相关问题