dongxu4580 2015-11-30 22:58
浏览 27

尝试编写查询以检索与索引值关联的多个项目

I am attempting to create a mysql query that will select all info from a table and then find all associative information from another table. In some instances there could be multiple items associated to the first table, and other other instances, there could be no items.

This is my query so far:

SELECT l.*, i.*
FROM lists l
LEFT JOIN images i ON (i.id = l.image_id AND i.users_id = l.user_id)
WHERE u.user_id = '2' 
    AND l.id = '1025' 
    AND l.active = '0'
ORDER BY i.id ASC

My issue is that when I fetch the data from the query, if there are multiple items in the images table that match the lists table, it returns duplicate entries.

I try to merge all of the data into one large array to use in my code:

while($row = $db->sql_fetchrow($result) )
{
    $lists[] = $row;
}

So this could be an example of result set:

Array
(
    [0] => Array
        (
            [id] => 12345
            [title] => The Old Man and the Sea
            [author] => Ernest Hemingway
            [image] => front.jpg
        )

    [1] => Array
        (
            [id] => 12345
            [title] => The Old Man and the Sea
            [author] => Ernest Hemingway
            [image] => back.jpg
        )

    [2] => Array
        (
            [id] => 12345
            [title] => The Old Man and the Sea
            [author] => Ernest Hemingway
            [image] => ernest.jpg
        )

    [3] => Array
        (
            [id] => 12345
            [title] => The Old Man and the Sea
            [author] => Ernest Hemingway
            [image] => book.jpg
        )

    [4] => Array
        (
            [id] => 12345
            [title] => The Old Man and the Sea
            [author] => Ernest Hemingway
            [image] => mary_hemingway.jpg
        )

    [5] => Array
        (
            [id] => 12331
            [title] => In Our Time
            [author] => Ernest Hemingway
            [image] => time.jpg
        )

    [6] => Array
        (
            [id] => 12331
            [title] => In Our Time
            [author] => Ernest Hemingway
            [image] => time_cover.jpg
        )

)

Is there a better way to write the query or process the data to ensure that I can display a single items with multiple images associated to it.

  • 写回答

2条回答 默认 最新

  • dongxiai3003 2015-11-30 23:08
    关注

    The query is fine, you just need to build a suitable data structure when you parse the results:

    $data = [];
    
    while($row = $db->sql_fetchrow($result) )
    {
        $id     = $row['id'];
        $title  = $row['title'];
        $author = $row['author'];
        $image  = $row['image'];
    
        if(isset($data[$id]){
           $data[$id]['images'][]=$image;
        }else{
           $data[$id] = [ 
               'title'  =>$title,
               'author' =>$author,
               'images' =>[$image],
           ];
        } 
    }
    
    var_dump($data);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看