0

当我想从数据库中选择数据时遇到问题。

这是我的查询

SELECT J.id as id_job, J.title as job_title, J.companies_id, C.name as company_name,J.created_at, L.name as Location,CL.postal_code, CL.address
                                FROM jobs J
                                INNER JOIN bla1 C ON ( C.id = J.company_id )
                                INNER JOIN bla2 CL ON ( CL.id = J.company_location_id )
                                INNER JOIN bla3 L ON ( L.id = CL.location_id )
                                WHERE J.status =1
                                AND C.status =2
                                And CL.status = 1
                                AND L.status = 1
                                ORDER BY J.created_at DESC
                                LIMIT 0 , 30

这是该查询的结果var_dump()

["_result":protected]=>
  array(15) {
    [0]=>
    array(8) {
      ["id_job"]=>
      string(2) "27"
      ["job_title"]=>
      string(10) "YUYUYUYUYU"
      ["company_id"]=>
      string(1) "3"
      ["company_name"]=>
      string(16) "voyage indonesia"
      ["created_at"]=>
      string(19) "2012-10-03 09:35:37"
      ["Location"]=>
      string(4) "Bali"
      ["postal_code"]=>
      string(6) "989767"
      ["address"]=>
      string(11) "dimana mana"
    }
    [1]=>
    array(8) {
      ["id_job"]=>
      string(2) "23"
      ["job_title"]=>
      string(11) "OYEEEEAAHHH"
      ["company_id"]=>
      string(1) "3"
      ["company_name"]=>
      string(16) "voyage indonesia"
      ["created_at"]=>
      string(19) "2012-09-28 09:06:54"
      ["Location"]=>
      string(7) "Bandung"
      ["postal_code"]=>
      NULL
      ["address"]=>
      NULL
    }
    [2]=>
    array(8) {
      ["id_job"]=>
      string(2) "20"
      ["job_title"]=>
      string(14) "TKPDKJKLADKSJA"
      ["company_id"]=>
      string(1) "3"
      ["company_name"]=>
      string(16) "voyage indonesia"
      ["created_at"]=>
      string(19) "2012-09-20 09:24:50"
      ["Location"]=>
      string(7) "Bandung"
      ["postal_code"]=>
      NULL
      ["address"]=>
      NULL
    }
}
["_total_rows":protected]=>
  int(3)
  ["_current_row":protected]=>
  int(0)
  ["_as_object":protected]=>
  bool(false)

我的目标:数组索引中的示例 8 像这样或比这个代码更好的东西

array(8) {
          ["id_job"]=>
          string(2) "27"
          ["job_title"]=>
          string(10) "YUYUYUYUYU"
          ["company_id"]=>
          string(1) "3"
          Array ( [companies] => array(1) {
          ["company_name"]=>
          string(16) "voyage indonesia"
          })
          ["created_at"]=>
          string(19) "2012-10-03 09:35:37"
          ["Location"]=>
          string(4) "Bali"
          ["postal_code"]=>
          string(6) "989767"
          ["address"]=>
          string(11) "dimana mana"
        }

company_id 是关系表数据库表 bla1,我想在关系表中给出数组,所以我可以很容易地解析数据,有array('bla1' = > array('company_id',1)); 什么建议吗?

4

2 回答 2

1

我想这就是你想要的。

 array[$row_id] = $row;
于 2012-10-05T10:12:08.897 回答
1

我看不到您可以从 SQL 查询中获取嵌套数组。数据库查询提供行和列而无需进一步的子结构。

您当然可以通过编程方式执行此操作,只需将包含作业和位置的表作为数组,然后从中生成所需的嵌套列表。但是,您将失去连接的力量,需要将其嵌入到您的代码中。

于 2012-10-05T10:29:54.610 回答