sql - 为逻辑句动态生成 SQL
问题描述
超级棘手的问题。我有一个客户想要true/false
基于代表rule
.
例如,如果规则是字符串:"56 AND 78"
,那么这意味着“true
如果Table A
有 ID56
和 ID的行,则返回78
。请注意,这并不意味着同一行有 ID 56
,78
因为这是不可能的。
现在,我可以继续为简单的情况动态生成一些伪 SQL,如下所示:
RETURN QUERY (SELECT EXISTS(SELECT 1
FROM table_a
WHERE id = 56)
AND
SELECT EXISTS(SELECT 1
FROM table_a
WHERE id = 78))
其中WHERE
子句直接从rule
. 这满足了简单的句子。
但是,规则可能会变得更加复杂。
例如"(43 OR 44 OR 47) AND (2182 OR 2179 OR 2183)"
,它用英语翻译为“表 A 是否有一行具有来自前三个 ID 之一的(任何)ID 和来自后三个 ID 的(任何)一个?”
sentence
随着 SQL变得越来越复杂,我正在努力寻找一种动态生成 SQL 的方法。
建议将不胜感激!
解决方案
It sounds as if your sentences
follow the following rules:
- A number is a small integer.
- A term is either a single number, or a set of terms either with AND between each pair, or OR between each pair.
- Terms must be enclosed by parentheses in order to be combined with more terms.
If this is the case, then I think you can simply:
- replace each number with
(SELECT EXISTS(SELECT 1 FROM table_a WHERE id = *number*))
- Leave any parentheses, ANDs and ORs where they are.
- Enclose the string in one more set of parentheses and prefix
RETURN QUERY
.
This certainly won't be the simplest SQL that expresses your sentence
but I think it will be equivalent.
If you want to optimize more, you can switch (5 or 6 or 7)
with (SELECT 1 FROM table_a WHERE id IN (5,6,7))
. But that shouldn't be necessary for a correct SQL translation.
推荐阅读
- vb.net - 安装 VS 2017 更新后表单设计器缺少控件
- backup - 如何控制 Emacs 将备份文件动态生成到哪个目录?
- android - 如何在 Android 应用中查找权限来源?
- regex - 如何在 Google 电子表格中使用正则表达式来提取和删除原始文本?
- linux - 如何将 find 和 grep 的结果输出为 filename => found
- c++ - 如何让 CMake 使用 Mingw-w64 gcc/g++?
- tfs - 在 TFS 中构建时收到错误消息
- r - 尽管对我的数据使用了 scale_x_discrete,但 X 轴标签不存在
- asp.net-mvc - 如何从 HttpContext 获取 JWT(使用 OpenIdConnect),并传递给 Azure AD Graph API
- html - 我的表单样式无法加载