简体   繁体   English

在连接中获取多行(codeigniter)

[英]Fetch multiple rows in join (codeigniter)

I am having trouble getting the results that I am expecting from the code below. 我无法从下面的代码中获得我期望的结果。

First Step is: 第一步是:

Fetch one row from a table an join multiple rows from another table... the rsult array should look like this: 从表中获取一行,从另一个表中连接多行...... 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
    )
  ) 
) 

My query looks something like this: 我的查询看起来像这样:

(it seems that the position of where, join, etc isnt important for the codigniters db class) (似乎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();

The issue that I'm facing is that I am only getting one row from the join when I am expecting to retrieve multiple rows per join. 我面临的问题是,当我希望每个连接检索多行时,我只从连接中获取一行。

EDIT: 编辑:

last_query(): 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

output: 输出:

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
        )

)

would like output (something like that would be cool): 想要输出(这样的东西会很酷):

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
            )
        )
)

Try grouping it by the event.id and then use group_concat, something like this: 尝试通过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

This will give you a comma separated list of all genres for a given event.id. 这将为您提供逗号分隔的给定event.id所有类型的列表。 You can change the delimiter(to something like a pipe) by using something like this 您可以使用类似的方式更改分隔符(类似于管道)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM