在连接中获取多行(codeigniter)

Posted

技术标签:

【中文标题】在连接中获取多行(codeigniter)【英文标题】:Fetch multiple rows in join (codeigniter) 【发布时间】:2012-08-07 20:37:20 【问题描述】:

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

第一步是:

从一个表中获取一行并从另一个表中连接多行... 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类并不重要)

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

【问题讨论】:

您是否尝试过运行针对 db 服务器构建的 sql 语句?根据您的 CI 代码,我假设它类似于 select * from events e join genres_x_events g on e.slug = g.event_slug where e.deleted = 0 and e.slug = 'SOMEVALUEHERE'; $this->db->last_query(); 的输出会有帮助。 是的。请发布您的实际输出,然后让我们了解您的预期输出。 我已经用最后一个查询、输出和我想要的输出更新了初始帖子;) 所以你得到了正确的数据,但它不是你想要的 php 结构。我有一种感觉,最好的方法是迭代你的结果集并将其工作到你想要的结构中。 【参考方案1】:

尝试按 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

【讨论】:

以上是关于在连接中获取多行(codeigniter)的主要内容,如果未能解决你的问题,请参考以下文章

如何使用codeigniter 4框架在mysql数据库中插入多行?

如何根据codeigniter中的时间戳获取数据?

如何在 Codeigniter 中更新共享相同外键的多行

尝试从 codeigniter 代码中的 2 个表中获取值

Codeigniter Active Record使用多个主键选择多行

使用 CodeIgniter 查询从同一列中选择多条记录