ds34222 2016-05-16 17:23
浏览 73
已采纳

无法弄清LEFT JOIN从左表获取所有内容并且不在MYSQLI中右侧重复结果

This is example of my code wich get's information from two mysql tables, rather easy stuff.

One table contains realisations and the other got types for realisations. Correlation left join would be realisations.id = realisations_kind.realisation_id, my actual code, has no left join, i have been doing it just a dirty way.

        $s = $mysqli->prepare( "SELECT id, name FROM realisations" );
        $s->execute();
        $s->bind_result( $id, $name );
        $s->store_result();
        while( $s->fetch())
        {
            /***/
            $si = $mysqli->prepare( "SELECT kind FROM realisations_kind WHERE realisation_id = ?" );
            $si->bind_param( 'i', $id );
            $si->execute();
            $si->bind_result( $kind );
            $si->store_result();
            while( $si->fetch())
            {
                if(isset($kind)) {
                    $arrayKind[] = $kind;
                }

            }

            $return[] = array('id' => $id, 'name' => $name, 'kind' => $arrayKind);
            $arrayKind = null;
            /***/
        }

This will output such array

Array
(
    [0] => Array
        (
            [id] => 1
            [name] => Building 1
            [kind] => Array
                (
                    [0] => 0
                    [1] => 1
                )

        )

    [1] => Array
        (
            [id] => 2
            [name] => Building 2
            [kind] => Array
                (
                    [0] => 1
                )

        )

    ..................more

so as you can see i got at index 0 key "kind" which got 2 results and at index 1 got "kind" 1 result, this is proper result because this table got 3 records two assigned to id1 and one to id2.

I have been trying to do this via left join and group sql statement but i can't find out how to do that, i did that several times in past but i forgot. Now i fill bit stupid to ask such basic thing there.

Left join results left table all rows result but duplicates right table or if i setup group by it remove duplicates from right but does not print out all results from left table, gives only first one :(

  • 写回答

1条回答 默认 最新

  • dsaxw4201 2016-05-16 18:02
    关注

    Of course LEFT JOIN gives duplicates in left table, that's the nature of joins. SQL does not return nested tables. All is combined into one single table. You have to structure the data in PHP.

    SELECT `r`.`id`, `r`.`name`, `k`.`kind`
    FROM
      `realisations` `r`
    LEFT JOIN
      `realisations_kind` `k`
    ON `k`.`realisation_id` = `r`.`id`
    ;
    

    PHP could have something like that

    while( $s->fetch())
    {
      if(!isset($data[$id]))
      { 
        $data[$id] =
        [ 'id'   => $id,
          'name' => $name,
          'kind' => [$kind]
        ]
      }
      else
      {
        $data[$id]['kind'][] = $kind;
      }
    }
    

    If you want a zero based numeric array, just do a $data = array_values($data) after the loop.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)