gpt4 book ai didi

sqlite - 将逗号分隔为 IN 子句的整数

转载 作者:行者123 更新时间:2023-12-01 16:25:16 24 4
gpt4 key购买 nike

我有三个表:估计、位置和部门。现在我正在连接表位置并估计以获得所需的结果。

查询

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

结果

enter image description here

对于上述要求,此查询工作正常。

现在我也想要相关的部门名称。所以我正在使用这个查询

查询

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

结果

enter image description here

这只给我提供了具有单一部门 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

结果

enter image description here

我尝试了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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com