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

时间:2015-04-22 01:48:31

标签: php mysql pdo

很抱歉这个标题可能听起来令人困惑,而且我之前已经完成了这项工作,但是在编码会议的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个查询来完成它

1 个答案:

答案 0 :(得分:1)

而不是$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;
}

应该可以为您提供更接近您想要的输出。不幸的是,你无法直接从数据库中获得这样的数据组织。