gpt4 book ai didi

php - 在连接中获取多行(codeigniter)

转载 作者:搜寻专家 更新时间:2023-10-31 22:12:00 24 4
gpt4 key购买 nike

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

第一步是:

从一个表中获取一行并连接另一个表中的多行...结果数组应如下所示:

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

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

编辑:

最后一个查询():

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

最佳答案

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

关于php - 在连接中获取多行(codeigniter),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11903418/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com