gpt4 book ai didi

php - 2个mysql表,单行链接到另一个表中的多个条目。我怎样才能只获得单行一次而不是每个其他条目

转载 作者:行者123 更新时间:2023-11-29 00:01:07 27 4
gpt4 key购买 nike

抱歉,标题可能听起来令人困惑,我以前做过,但是在编码 session 中进行了 14 个小时后,我被卡住了,我的大脑准备好爆炸了。

我有一个包含 ID、名称和描述的表,以及一个包含多个链接到第一个表 ID 的条目的表。

我想返回一个数组,其中包含第一个表的描述和名称,然后是它链接到数组中下一个的所有条目。

下面的代码返回一个我可以循环遍历的数组,但单行的描述和名称在所有其他条目中。

    $stmt = $db->prepare('
SELECT
cl.name,
cl.description,
cf.*
FROM
contentList as cl
LEFT JOIN
contentFiles as cf
ON
cl.contentid = cf.contentid

WHERE cl.contentid = :contentid
');
$stmt->bindValue(':contentid', $contentid);
$stmt->execute();

$content = $stmt->fetchAll();

这将返回这个,这将有望解释我不希望它做的事情

    [0] => Array
(
[name] => First List yea
[0] => First List yea
[description] => This is my first document
[1] => This is my first document
[fileid] => 28
[2] => 28
[fileName] => 22_project_plan.pdf
[3] => 22_project_plan.pdf
[fileSize] => 1694506
[4] => 1694506
[fileType] => applicatio
[5] => applicatio
[deleted] => 1
[6] => 1
[contentid] => 8
[7] => 8
[userid] => 22
[8] => 22
[fileDate] => 0000-00-00 00:00:00
[9] => 0000-00-00 00:00:00
)

[1] => Array
(
[name] => First List yea
[0] => First List yea
[description] => This is my first document
[1] => This is my first document
[fileid] => 29
[2] => 29
[fileName] => 22_about.jpg
[3] => 22_about.jpg
[fileSize] => 213162
[4] => 213162
[fileType] => image/jpeg
[5] => image/jpeg
[deleted] => 1
[6] => 1
[contentid] => 8
[7] => 8
[userid] => 22
[8] => 22
[fileDate] => 0000-00-00 00:00:00
[9] => 0000-00-00 00:00:00
)

[2] => Array
(
[name] => First List yea
[0] => First List yea
[description] => This is my first document
[1] => This is my first document
[fileid] => 30
[2] => 30
[fileName] => 22_arrow.png
[3] => 22_arrow.png
[fileSize] => 18059
[4] => 18059
[fileType] => image/png
[5] => image/png
[deleted] => 1
[6] => 1
[contentid] => 8
[7] => 8
[userid] => 22
[8] => 22
[fileDate] => 0000-00-00 00:00:00
[9] => 0000-00-00 00:00:00
)

抱歉解释不当,在此先感谢您。

对不起,我希望它的输出类似于

        [name] => First List yea
[0] => First List yea
[description] => This is my first document

[0] => Array
(
[fileid] => 28
[2] => 28
[fileName] => 22_project_plan.pdf
[3] => 22_project_plan.pdf
[fileSize] => 1694506
[4] => 1694506
[fileType] => applicatio
[5] => applicatio
[deleted] => 1
[6] => 1
[contentid] => 8
[7] => 8
[userid] => 22
[8] => 22
[fileDate] => 0000-00-00 00:00:00
[9] => 0000-00-00 00:00:00
)

[1] => Array
(
[fileid] => 29
[2] => 29
[fileName] => 22_about.jpg
[3] => 22_about.jpg
[fileSize] => 213162
[4] => 213162
[fileType] => image/jpeg
[5] => image/jpeg
[deleted] => 1
[6] => 1
[contentid] => 8
[7] => 8
[userid] => 22
[8] => 22
[fileDate] => 0000-00-00 00:00:00
[9] => 0000-00-00 00:00:00
)

[2] => Array
(
[1] => This is my first document
[fileid] => 30
[2] => 30
[fileName] => 22_arrow.png
[3] => 22_arrow.png
[fileSize] => 18059
[4] => 18059
[fileType] => image/png
[5] => image/png
[deleted] => 1
[6] => 1
[contentid] => 8
[7] => 8
[userid] => 22
[8] => 22
[fileDate] => 0000-00-00 00:00:00
[9] => 0000-00-00 00:00:00
)

IE 多维数组,我想我可以用 JOIN 而不是 2 个查询来完成

最佳答案

代替 $content = $stmt->fetchAll(PDO::FETCH_ASSOC);
尝试:

$content = array();
while($row = $stmt->fetch()) {
if(!isset($content[$row['name']])) {
$content[$row['name']] = array ('description' => $row['description'], data => array());
}
$content[$row['name']]['data'][] = $row;
}

这应该会给你一个更接近你想要的输出。不幸的是,您无法直接从数据库中获取这样组织的数据。

关于php - 2个mysql表,单行链接到另一个表中的多个条目。我怎样才能只获得单行一次而不是每个其他条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29786314/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com