我有这样的表
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显示这样的数据,我真的很困惑,有人请帮助我!
答案 0 :(得分:0)
编辑:使用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循环方法来循环这个。
使用mysql GROUP_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