简体   繁体   中英

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:

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)

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

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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