【问题标题】:How to select multiple data from other table from each id separated by comma in codeigniter如何从codeigniter中用逗号分隔的每个id中选择其他表中的多个数据
【发布时间】:2016-02-24 18:37:35
【问题描述】:

我有这样的表

tbl_post

+-----------+--------------+
| post_id   | post_content |
+-----------+--------------+
| 1         |  contentone  |
+-----------+--------------+

tbl_category

+-------------+---------------+
| category_id | category_name |
+-------------+---------------+
|           1 | Politic       |
|           2 | Social        |
|           3 | Economy       |
+-------------+---------------+

tbl_category_post

+------------------+-------------+---------+
| category_post_id | category_id | post_id |
+------------------+-------------+---------+
|                1 |           1 |       1 |
|                2 |           2 |       1 |
|                3 |           3 |       1 |
+------------------+-------------+---------+

那么我想要这样的输出

+--------------+--------------------------+
| post_content |         category         |
+--------------+--------------------------+
|            1 | Politic, Social, Economy |
+--------------+--------------------------+

然后如何使用codeigniter显示这样的数据,我真的很困惑,请任何人帮助我!

【问题讨论】:

标签: php mysql codeigniter


【解决方案1】:

编辑:使用 Codeigniter(未测试):

$this->db->select('post_id, GROUP_CONCAT(tc.category_name) AS category_name')
->from('tbl_category_post tcp')
join->('tbl_category tc', 'tc.category_id=tcp.category_id', 'left')
->group_by('tcp.post_id');

我想你需要 PHP 循环方法来循环这个。

使用mysqlGROUP_CONCAT函数:

SELECT post_id, GROUP_CONCAT(tc.category_name) AS category_name
FROM tbl_category_post tcp
LEFT JOIN tbl_category tc ON tc.category_id=tcp.category_id
GROUP BY tcp.post_id

【讨论】:

  • 嗨,艾哈迈德,然后如何将其创建到 codeigniter 活动记录中,以及如何循环 foreach,无论如何,在它在 mysql 上工作之前已经完成了。
  • 谢谢兄弟,你救了我的命:)
猜你喜欢
  • 2016-10-02
  • 2013-03-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多