简体   繁体   English

对于一组sql查询,如何确定哪个结果集包含某个行?

[英]For a set of sql-queries, how do you determine which result-set contains a certain row?

I have a set of sql - queries: 我有一组sql-查询:

List<String> queries = ...
queries[0] = "select id from person where ...";
...
queries[8756] = "select id from person where ...";

Each query selects rows from the same table 'person'. 每个查询从同一表“ person”中选择行。 The only difference is the where-clause. 唯一的区别是从句。 Table 'person' looks like this: 表“ person”看起来像这样:

   id | name | ... many other columns

How can i determine which queries will contain a certain person in it's result set? 我如何确定哪些查询的结果集中将包含某个人?

For example: 例如:

List<Integer> matchingQueries = magicMethod(queries, [23,45]); 

The list obtained by 'magicMethod' filters all sql queries present in the list 'queries' (defined above) and returns only those that contain either the person with id 23 OR a person with id 45. 通过“ magicMethod”获得的列表将过滤列表“ queries”(上面定义)中存在的所有sql查询,并仅返回包含id为23的人或id为45的人的查询。

Why i need it: I am dealing with an application that contains products and categories where the categories are sql queries that define which products belong to them (queries stored in a table also). 我为什么需要它:我正在处理一个包含产品和类别的应用程序,其中类别是sql查询,这些查询定义了哪些产品属于它们(查询也存储在表中)。 Now i have a requirement where an admin has to see all categories an item belongs to immediately after the item was created. 现在我有一个要求,管理员必须在创建项目后立即查看该项目所属的所有类别。 Btw, over 8.000 categories defined (so far, more to come). 顺便说一句,定义了超过8000个类别(到目前为止,还会有更多类别)。

language and db: java && postgreSQL 语言和数据库:java && postgreSQL

Thanks, 谢谢,

I can think of 2 ways. 我可以想到两种方式。 First way is to create a value object Query which contains two properties: a Long id query ID and a List<Person> results. 第一种方法是创建一个包含两个属性的值对象QueryLong id查询ID和List<Person>结果。 Another way is to include the query ID in the results. 另一种方法是在结果中包含查询ID。 Eg 例如

queries[0] = "SELECT id, name, etc, 0 AS query FROM person WHERE ...";
// ...
queries[8756] = "SELECT id, name, etc, 8756 AS query FROM person WHERE ...";

or 要么

queries[0] = "SELECT id, name, etc, %d AS query FROM person WHERE ...";
// ...
queries[8756] = "SELECT id, name, etc, %d AS query FROM person WHERE ...";

for (int i = 0; i < queries.length; i++) {
    String sql = String.format(queries[i], i);
    // ...
}

so that you can re-obtain it by 这样您可以通过以下方式重新获取它

int queryId = resultSet.getInt("query");

That said, this is all with all a bit a smell ( DRY ). 就是说,所有这些都有一点气味( DRY )。 I'd recommend to rethink the whole approach. 我建议重新考虑整个方法。 Feel free to ask it as a new question, "I have functional requirement XYZ, I have approached it as follows. Is it right?" 随便问一个新问题: “我有功能要求XYZ,我已经按照以下方式进行了处理。对吗?” .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM