我有一个场景,我有表名为组和论坛。在该组中,我想要以降序显示在该组内创建的最大论坛的5个组。现在的问题是,我有计算每个组中有多少个论坛,现在的主要问题是我很困惑如何获得该组的数据。谁能帮我吗 ?获得第二张桌子的ID号
<?php
$sql_gr_coun = $this->db->query("SELECT groups.*, (SELECT count(group_id) FROM forums WHERE groups.id = forums.group_id) as forumcount FROM groups ORDER BY forumcount DESC LIMIT 5");
foreach($sql_gr_coun->result() as $data_count):
$sql_grp = $this->db->get_where('groups', array('id' => $data_count->group_id));
endforeach;
?>
Groups
+----+----------+
| id | name |
+----+----------+
| 1 | group 1 |
| 2 | group 2 |
| 3 | group 3 |
| 4 | group 4 |
+----+----------+
forums
+------------------+-------------+
| id | title | group_id |
+------------------+-------------+
| 1 | test 1 | 2 |
| 2 | test 2 | 3 |
| 3 | test 3 | 2 |
| 4 | test 4 | 3 |
| 5 | test 5 | 2 |
| 6 | test 6 | 4 |
+------------------+-------------+
由于我无法现在该怎么做呢
获得组ID ,将它发送给某个模型函数并返回数据 –
我如何获得群组ID,其中有更多的论坛经历循环第一组ID为3和第二组ID为2,所以如何获得3论坛组信息和2论坛组信息? –
你能分享你的数据库模式吗? ?你的表结构? –