我有以下查询:
$this->db
->select('SQL_CALC_FOUND_ROWS null as rows
,services.*
,service_categories.*
,categories.*
,table4.*', FALSE)
->from('services')
->where('services.user_id', $user_id)
->join('service_categories', 'service_categories.service_id = services.service_id')
->join('categories', 'categories.cat_id= service_categories.cat_id')
->join('table4', 'table4.service_id= services.service_id', 'left')
->group_by('services.service_id')
->order_by('services.service_id', 'DESC');
$query = $this->db->get();
表格如下所示:
Table: services
------------------------------------------------
| service_id | other_service_columns | user_id |
-----------------------------------------------|
| 23 | other data | 14 |
| 24 | other data | 14 |
------------------------------------------------
Table: service_categories
---------------------------------------
| service_cat_id | cat_id | service_id|
---------------------------------------
| 1 | 924 | 23 |
| 2 | 863 | 23 |
| 3 | 506 | 24 |
| 4 | 510 | 24 |
---------------------------------------
Table: categories
---------------------
| cat_id | cat_name |
---------------------
| 924 | cleaning |
| 863 | washing |
| 506 | drying |
| 510 | scrubbing|
---------------------
表 4 可能没有任何结果,因此我正在执行左连接。但是,当我按 service_id 分组时,我没有从“service_categories”和“categories”表中获取所有相关类别 - 查询中仅返回一行。 group_concat 是答案吗?如果是这样,我不确定如何使用它来获取我需要的结果 - 即查询中返回的所有相关类别。请帮忙!
我需要能够通过使用 foreach 迭代查询结果来处理查询结果,以在 View 中生成如下内容:
--------------------------------------------------------
| Service ID | Other Service Data | Service Categories |
--------------------------------------------------------
| 23 | Data for Service 23| cleaning, washing |
| 24 | Data for Service 24| drying, scrubbing |
--------------------------------------------------------
最佳答案
更新:根据您的评论和更新的问题,您的基本 SQL 查询可能如下
SELECT s.*, q.categories, t4.*
FROM services s LEFT JOIN
(
SELECT sc.service_id, GROUP_CONCAT(c.cat_name) categories
FROM service_categories sc JOIN categories c
ON sc.cat_id= c.cat_id
GROUP BY sc.service_id
) q ON s.service_id = q.service_id LEFT JOIN table4 t4
ON s.service_id = t4.service_id
WHERE s.user_id = ?
示例输出:
| SERVICE_ID | OTHER_SERVICE_COLUMNS | USER_ID | CATEGORIES | ------------------------------------------------------------------- | 23 | other data | 14 | cleaning,washing | | 24 | other data | 14 | drying,scrubbing |
Here is SQLFiddle demo
Now a possible version of CI code
$sql = "SELECT s.*, q.categories, t4.*
FROM services s LEFT JOIN
(
SELECT sc.service_id, GROUP_CONCAT(c.cat_name) categories
FROM service_categories sc JOIN categories c
ON sc.cat_id= c.cat_id
GROUP BY sc.service_id
) q ON s.service_id = q.service_id LEFT JOIN table4 t4
ON s.service_id = t4.service_id
WHERE s.user_id = $user_id";
$query = $this->db->query($sql);
foreach ($query->result() as $row) {
...
}
原始问题的答案: 恕我直言,您的查询有几个问题
- 您将结果集限制为唯一带有
WHERE
子句的service_id
。因此,GROUP BY services.service_id
和ORDER BY services.service_id
没有任何意义。仅当您想要将值打包到分隔字符串并返回唯一的行时,GROUP_BY
才有意义。 - 您没有使用
LIMIT
,因此SQL_CALC_FOUND_ROWS
也没有任何意义。
话虽如此,您的查询可能看起来像
$result = $this->db
->select('services.*
,service_categories.*
,categories.*
,table4.*', FALSE)
->from('services')
->join('service_categories', 'service_categories.service_id = services.service_id')
->join('categories', 'categories.cat_id= service_categories.cat_id')
->join('table4', 'table4.service_id= services.service_id', 'left')
->where('services.service_id', $service_id)
->get();
这里是SQLFiddle 演示
关于mysql - Codeigniter Active Record/MySQL 查询 - group_by 导致仅返回一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17634003/