php sql连接查询多数组合并内容

xyhw6mcr  于 2021-06-23  发布在  Mysql
关注(0)|答案(2)|浏览(241)

我有一个sql查询的问题,我想继续与php。以下是我的数据库结构:
表:截面

+------------+---------------+-----------------+--+--+
| section_id | section_titel | section_text    |  |  |
+------------+---------------+-----------------+--+--+
| 1          | Section One   | Test text blaaa |  |  |
+------------+---------------+-----------------+--+--+
| 2          | Section Two   | Test            |  |  |
+------------+---------------+-----------------+--+--+
| 3          | Section Three | Test            |  |  |
+------------+---------------+-----------------+--+--+

表:分项

+----------------+-------------------+------------------+-----+--+
| sub_section_id | sub_section_titel | sub_section_text | sId |  |
+----------------+-------------------+------------------+-----+--+
| 1              | SubOne            | x1               | 1   |  |
+----------------+-------------------+------------------+-----+--+
| 2              | SubTwo            | x2               | 1   |  |
+----------------+-------------------+------------------+-----+--+
| 3              | SubThree          | x3               | 3   |  |
+----------------+-------------------+------------------+-----+--+

这些部分通过第二个表中的“sid”链接起来(subï部分)。因此,标题为“subone”的子小节是id为1的节(来自节表)中的子sub。
我使用此查询获取结果:

SELECT section_titel as t1, sub_section_titel as t2 
FROM sections LEFT JOIN sub_sections ON section_id = sId;

我的输出如下所示:

Array
(
    [0] => Array
        (
            [t1] => Section One
            [t2] => SubOne
        )

    [1] => Array
        (
            [t1] => Section One
            [t2] => SubTwo 
        )
)

所以问题是,我得到了表“sections”的多个结果。我需要这样的结果:

Array
(
    [0] => Array
        (
            [t1] => Section One
            [t2] => Array
                (
                    [0] => SubOne
                    [1] => SubTwo

                )

        )

)

有什么办法吗?提前多谢!
谢谢,j。能源部;)

lawou6xi

lawou6xi1#

您可以使用php和mysql的组合来实现这一点。将查询更改为:

SELECT section_titel as t1, GROUP_CONCAT(sub_section_titel) as t2 
FROM sections LEFT JOIN sub_sections ON section_id = sId
GROUP BY t1
HAVING t2 IS NOT NULL

这将为您提供如下结果表:

t1              t2
Section One     SubOne,SubTwo
Section Three   SubThree

(如果你想知道 Section Two ,移除 HAVING t2 IS NOT NULL (来自查询的条件)
然后在php中(我假设 mysqli 有联系的 $conn )

$result = mysqli_query($conn, $sql) or die(mysqli_error($conn));
$out = array();
while ($row = mysqli_fetch_array($result)) {
   $out[] = array('t1' => $row['t1'], 't2' => explode(',', $row['t2']));
}
print_r($out);

输出:

Array
(
    [0] => Array
        (
            [t1] => Section One
            [t2] => Array
                (
                    [0] => SubOne
                    [1] => SubTwo
                )    
        )

    [1] => Array
        (
            [t1] => Section Three
            [t2] => Array
                (
                    [0] => SubThree
                )
        )
)
4xrmg8kj

4xrmg8kj2#

你有你需要的数据数组。如果您只想以不同的方式呈现它,您可以遍历它并生成您想要的。我在用 $arrayInput 与您提供的结构相同。

$arrayOutput = array();
foreach ($arrayInput as $itemInput) {
    $arrayOutput[$itemInput['t1']]['t1'] = $itemInput['t1'];
    $arrayOutput[$itemInput['t1']]['t2'][] = $itemInput['t2'];
}

echo "<pre>";
print_r($arrayOutput);

相关问题