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

2 mysql tables, a single row links to multiple entries in another table. how can I only get the single row once rather than for every other entry

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

我有一个 table 和一个 table,它有多个链接到第一个 table id 的 table。

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

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

    $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();

这 return 这有望解释我不希望它做的事情

    [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;
}

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