我有三个表估计,位置和部门。现在我正在加入表格位置并进行估算以获得所需的结果。
查询
SELECT e.id, e.department_ids FROM estimate e JOIN location l ON e.location_id = l.id WHERE e.user_id = '1' and e.delete_flag = 0 and l.active_flag = 1
结果
对于上述要求,此查询工作正常。
现在我也想要相关的部门名称。所以我正在使用此查询
查询
SELECT e.id, e.department_ids, (SELECT group_concat(department, ', ') FROM department WHERE id IN (e.department_ids)) as departmentName FROM estimate e JOIN location l ON e.location_id = l.id WHERE e.user_id = '1' and e.delete_flag = 0 and l.active_flag = 1
结果
这只给了我一个部门ID的部门。
虽然如果我将e.department硬编码为“2,5”,我会得到理想的结果
查询
SELECT e.id, e.department_ids, (SELECT group_concat(department, ', ') FROM department WHERE id IN (2, 5)) as departmentName FROM estimate e JOIN location l ON e.location_id = l.id WHERE e.user_id = '1' and e.delete_flag = 0 and l.active_flag = 1
结果
我尝试了强制转换(e.department_ids为整数),但这也是每行占用一个department_id。是否有任何函数可以构建整个e.departments字符串(即“4,2”),以便我可以在IN子句中传递它?
我在oracle中得到了相同的解决方案,我发现它与sqlite相同。
答案 0 :(得分:1)
我使用GROUP BY
子句获得了所需的结果。