如何解决创建SQL查询以使用动态where子句/搜索条件在多个单向一对多表中进行搜索
我有四个桌子。一个是main_table
,还有其他三个表是通过main_table进行一对多映射的。 (如果我的解释不正确,请提供更正)
child_1,child_2,child_3
与unique_key
中的main_table
映射
(我要连接的数据库是postgres
。)
main_table (
unique_key character varying(100) PRIMARY KEY,--few more fields
);
child_1 (
id BIGSERIAL PRIMARY KEY,name character varying(255),main_table_unique_key character varying(100) REFERENCES main_table(unique_key) NOT NULL
);
child_2 (
id BIGSERIAL PRIMARY KEY,main_table_unique_key character varying(100) REFERENCES main_table(unique_key) NOT NULL
);
child_3 (
id BIGSERIAL PRIMARY KEY,type character varying(255),main_table_unique_key character varying(100) REFERENCES main_table(unique_key) NOT NULL
);
我有多个动态搜索条件。因此,基于此,我将生成SQL内部联接查询以获取记录。 搜索条件的数据结构如下:
[
{
"child_1": "Dollar Sales","child_2": "Retailer","child_3": [
{
"type": "XYZ","name": "name abc"
},{
"type": "foo","name": "bar"
}
]
},{
"child_1": "EUR Sales ","child_2": null,"name": "name pqr"
}
]
},{
"child_1": "USD Sales","child_2": "State","child_3": []
}
]
我将每个顶级数组项称为单个search criteria
。因此,在上面的示例中,这3 search criteria
。
child_1
不能为null。但是child_2
可以是,child_3
可以有零个或多个记录。
如果我要在单个项目中有1 or more child_3
的情况下检索结果,那么我必须与之匹配的是,在unique_key
表中只有child_3
出现的记录数量如此之多。
我对SQL不太好,所以目前我正在做的是将child_3展平。因此,如果child_2
中有两个项目,那么我要为第一个数组项目创建两个搜索条件。
我当前的JAVA + SQL方法是: 这是我实现它的方式:
- 进行具有
child_1= Dollar Sales && child_2 = Retailer && type = XYZ && name= name abc
的内部联接查询 - 将结果从1存储到
resultArray
。 - 进行具有
child_1= Dollar Sales && child_2 = Retailer && type = foo && name= bar
的内部联接查询 - 将3中的结果存储到
resultArray
中。 - 按
resultArray
和count = child_3.size()
分组 - 从
resultArray
中选择满足5个条件的记录到finalResultArray
中。 - 按照我要针对每个搜索条件执行的步骤。
如果您需要我的代码,如下所示:
List<ResponseItem> multipleChild3Response = search.getChild3()
.stream()
.map(child3 -> buildAndRetrieve(search.child1(),search.child2(),child3))
.flatMap(Collection::stream)
.collect(Collectors.toList());
Set<String> matchingUniqueKey = multipleChild3Response
.stream()
.collect(Collectors.groupingBy(ResponseItem::getUniqueKey,Collectors.counting()))
.entrySet()
.stream()
.filter(entry -> entry.getValue() == noOfBos)
.map(Map.Entry::getKey)
.collect(Collectors.toSet());
if (!matchingUniqueKey.isEmpty()) {
List<ResponseItem> matchingChild3Result = matchingUniqueKey
.stream()
.map(uniqueKey -> multipleChild3Response
.stream()
.filter(mainTableObj -> mainTableObj.getUniqueKey().equalsIgnoreCase(uniqueKey))
.findAny()
)
.filter(Optional::isPresent)
.map(Optional::get)
.collect(Collectors.toList());
finalResultArray.addAll(matchingChild3Result);
}
buildAndRetrieve
函数生成如下的SQL查询:
select n.unique_key,n.document,--few more fields only from main_table
from main_table n
inner JOIN child_1 nm on n.unique_key = nm.main_table_unique_key
--inner JOIN child_2 na on n.unique_key = na.main_table_unique_key --this is selected if child_2 is not null
inner join child_3 nbo on n.unique_key = nbo.main_table_unique_key --this is selected if child_3 is not null
where ( nm.name='Dollar Sales' and na.name='Retailer' nbo.type='XYZ' and nbo.name='name abc');
目前,我正在使用JAVA(主要是这种方法)和SQL方法来解决上述问题。
如何使用某些SQL
或with
将以上JAVA逻辑完全写入joins
中?这将帮助我在single SQL query
中检索结果,而不是多次触发查询。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。