在连接中获取多行(codeigniter)

时间:2012-08-10 14:08:38

标签: php mysql codeigniter join

我无法从下面的代码中获得我期望的结果。

第一步是:

从表中获取一行,从另一个表中连接多行...... rsult数组应该如下所示:

array(
  field_1, 
  field_2, 
  field_3, 
  joined_array(
    field_a_array(
      field_a_a, 
      field_a_b, 
      field_a_c
    ), 
    field_b_array(
      field_b_a, 
      field_b_b, 
      field_b_c
    )
  ) 
) 

我的查询看起来像这样:

(似乎where,join等位置对于codigniters db class来说并不重要)

$this->db->select('events.*, genres_x_events.*');

$this->db->from('events');
$this->db->where('events.slug', $slug);
$this->db->where('events.deleted', 0);

$this->db->join('genres_x_events', 'genres_x_events.event_slug = events.slug');

$query = $this->db->get();

我面临的问题是,当我希望每次连接检索多行时,我只从连接中获取一行。

编辑:

last_query():

SELECT `events`.*, `genres_x_events`.`genre_slug`
FROM (`events`)
LEFT OUTER JOIN `genres_x_events` ON `genres_x_events`.`event_slug` = `events`.`slug`
WHERE `events`.`slug` =  'test'
AND `events`.`deleted` =  0

输出:

Array(
    [0] => Array
        (
            [id] => 25
            [headline] => test
            [subheadline] => 
            [slug] => test
            [date] => 2012-08-10
            [start_time] => 00:00:00
            [end_time] => 00:00:00
            [price] => 
            [body] => 
            [location_id] => 5
            [genre_id] => 0
            [creation_date] => 2012-08-10 14:26:33
            [update_date] => 2012-08-10 14:26:41
            [deleted] => 0
            [genre_slug] => rock
        )

    [1] => Array
        (
            [id] => 25
            [headline] => test
            [subheadline] => 
            [slug] => test
            [date] => 2012-08-10
            [start_time] => 00:00:00
            [end_time] => 00:00:00
            [price] => 
            [body] => 
            [location_id] => 5
            [genre_id] => 0
            [creation_date] => 2012-08-10 14:26:33
            [update_date] => 2012-08-10 14:26:41
            [deleted] => 0
            [genre_slug] => metal
        )

    [2] => Array
        (
            [id] => 25
            [headline] => test
            [subheadline] => 
            [slug] => test
            [date] => 2012-08-10
            [start_time] => 00:00:00
            [end_time] => 00:00:00
            [price] => 
            [body] => 
            [location_id] => 5
            [genre_id] => 0
            [creation_date] => 2012-08-10 14:26:33
            [update_date] => 2012-08-10 14:26:41
            [deleted] => 0
            [genre_slug] => indie
        )

)

想要输出(类似的东西会很酷):

Array(
    [0] => Array
        (
            [id] => 25
            [headline] => test
            [subheadline] => 
            [slug] => test
            [date] => 2012-08-10
            [start_time] => 00:00:00
            [end_time] => 00:00:00
            [price] => 
            [body] => 
            [location_id] => 5
            [genre_id] => 0
            [creation_date] => 2012-08-10 14:26:33
            [update_date] => 2012-08-10 14:26:41
            [deleted] => 0
            [genres] => Array(
                [0] => rock
                [1] => metal
                [2] => indie
            )
        )
)

1 个答案:

答案 0 :(得分:0)

尝试通过event.id对其进行分组,然后使用group_concat,如下所示:

SELECT `events`.*, group_concat(`genres_x_events`.`genre_slug`)
FROM (`events`)
LEFT OUTER JOIN `genres_x_events` ON `genres_x_events`.`event_slug` = `events`.`slug`
WHERE `events`.`slug` =  'test'
AND `events`.`deleted` =  0
GROUP BY events.id

这将为您提供逗号分隔的给定event.id所有类型的列表。您可以使用类似

之类的内容更改分隔符(类似于管道)
SELECT `events`.*, group_concat(`genres_x_events`.`genre_slug` SEPARATOR '|')
FROM (`events`)
LEFT OUTER JOIN `genres_x_events` ON `genres_x_events`.`event_slug` = `events`.`slug`
WHERE `events`.`slug` =  'test'
AND `events`.`deleted` =  0
GROUP BY events.id