作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有三个表:估计、位置和部门。现在我正在连接表位置并估计以获得所需的结果。
查询
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
结果
我尝试了cast(e.department_ids as integer),但这也每行采用单个department_id。是否有任何函数可以转换整个 e.departments 字符串(即“4, 2”),以便我可以在 IN 子句中传递它?
我在 oracle 中找到了相同的解决方案,我发现它与 sqlite 等效。
最佳答案
我使用 GROUP BY
子句得到了所需的结果。
关于sqlite - 将逗号分隔为 IN 子句的整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25488444/
我是一名优秀的程序员,十分优秀!