PHP SQL连接查询在多数组中合并内容
我的SQL查询有问题,我想继续使用PHP。 以下是我的数据库结构: 表格:章节PHP SQL连接查询在多数组中合并内容,php,mysql,sql,join,Php,Mysql,Sql,Join,我的SQL查询有问题,我想继续使用PHP。 以下是我的数据库结构: 表格:章节 +------------+---------------+-----------------+--+--+ | section_id | section_titel | section_text | | | +------------+---------------+-----------------+--+--+ | 1 | Section One | Test text blaa
+------------+---------------+-----------------+--+--+
| 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 | |
+----------------+-------------------+------------------+-----+--+
表格:子章节
+------------+---------------+-----------------+--+--+
| 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”链接。
因此,带有titel“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
)
)
t1 t2
Section One SubOne,SubTwo
Section Three SubThree
所以问题是,我得到了表“sections”的多个结果。我需要这样的结果:
Array
(
[0] => Array
(
[t1] => Section One
[t2] => Array
(
[0] => SubOne
[1] => SubTwo
)
)
)
有没有办法做到这一点?非常感谢
谢谢,
J.能源部;) 您可以通过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
这将为您提供如下结果表:
Array
(
[0] => Array
(
[t1] => Section One
[t2] => SubOne
)
[1] => Array
(
[t1] => Section One
[t2] => SubTwo
)
)
t1 t2
Section One SubOne,SubTwo
Section Three SubThree
(如果您想要第二节的结果
,请从查询中删除HAVING t2 IS NOT NULL
条件)
然后在您的PHP中(我假设mysqli
带有连接$conn
)
输出:
Array
(
[0] => Array
(
[t1] => Section One
[t2] => Array
(
[0] => SubOne
[1] => SubTwo
)
)
[1] => Array
(
[t1] => Section Three
[t2] => Array
(
[0] => SubThree
)
)
)
您拥有包含所需数据的阵列。如果您只想以不同的方式呈现它,那么可以对它进行迭代并生成所需的内容。我使用的是与您提供的结构相同的
$arrayInput
$arrayOutput = array();
foreach ($arrayInput as $itemInput) {
$arrayOutput[$itemInput['t1']]['t1'] = $itemInput['t1'];
$arrayOutput[$itemInput['t1']]['t2'][] = $itemInput['t2'];
}
echo "<pre>";
print_r($arrayOutput);
$arrayOutput=array();
foreach($arrayInput作为$itemInput){
$arrayOutput[$itemInput['t1']['t1']=$itemInput['t1'];
$arrayOutput[$itemInput['t1']['t2'][]=$itemInput['t2'];
}
回声“;
打印(arrayOutput);
是的,只需将一维数组转换为多维数组您是否希望没有子节的第2节有任何输出?如果您询问查询是否会输出这样的数组,答案是:否。但是您可以轻松地迭代此结果并创建所需的数组。我要写一个例子,我不认为我只能转换这个输出,因为在这些数据中实际上没有链接逻辑。我想要第二节的一个输出,只有一个空数组“t2”。谢谢你们的帮助。标题不是链接逻辑吗?嗯,我对你的问题投了赞成票,因为这是显示表格数据的好方法。:)我编辑了我的答案,因为我好像在说你的答案。不是我的意思,对不起@拉斐尔:没问题,我想我们都有很好但不同的答案。谢谢你们的帮助。我是这样做的:)但我有另一个小问题。我在这个表中还有一些列,它们保存了两个表的显示顺序。在“节”表中,它的排序规则和在子节表中,它的“iorder”。如果我尝试显示sorder列,它每次都会显示0。我编辑我的查询如下:选择section_titel作为t1,group_concat(sub_section_titel)作为t2,tb1.sorder作为sections tb1中的sorder LEFT JOIN sub_sections tb2在tb1上。section_id=tb2.sid既然这个问题解决了,你可以发布另一篇关于数据排序的文章吗?如果你用这个答案修改了代码,伙计,你应该选择这个问题的答案。:)严格地说,此代码不会给出所需的输出OP,因为顶级数组键是第一节
,第二节
等。而不是0
,1
等。通常,数组的键是数字,用于迭代目的,而不是索引,采用索引作为标题仅仅是使用它们以尽可能少的代码量转换为所需的输出数据。我认为主要的阵列键是技术性的。