我有一个类似 SQL 的示例
SELECT id, age, department
FROM User
WHERE
age < 50 AND id > 100 AND
(
department = 'IT' OR department = 'DEVOPS' OR department = 'ACCOUNTING' .... more OR conditions on department
)
我需要将这个SQL转换为多个SQL,并通过UNION ALL组合起来(其中保留原来的逻辑)。
期望的结果是
(
SELECT id, age, department
FROM User
WHERE age < 50 AND id > 100 AND department = 'IT'
)
UNION ALL
(
SELECT id, age, department
FROM User
WHERE age < 50 AND id > 100 AND department = 'DEVOPS'
)
UNION ALL
(
SELECT id, age, department
FROM User
WHERE age < 50 AND id > 100 AND department = 'ACCOUNTING'
)
UNION ALL
....further conditions
位背景
我们使用 PRESTO DB 作为 SQL 引擎。 我们发现 1 个限制,其中如果查询很大且具有超过 1000 个 AND/OR 条件,则查询会失败。 解决方案之一是通过 UNION 减少条件。 https://support.treasuredata.com/hc/en-us/articles/360001457447-Presto-Query-FAQs
这可以通过使用 SQL 解析器库以编程方式实现吗?
最佳答案
使用in
和not Between
select *
from myTable
where department in ('IT', 'Finance', 'etc')
and not age between 50 and 100
关于java - 将较大的 SQL 转换为通过 UNION ALL 组合的多个较小的 SQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59627939/