简体   繁体   English

我怎么知道一个子句查询结果包含另一个子句查询结果?

[英]How can I know a where-clause query results contains another where-clause query results?

How can I know a where-clause's query-results contains another where-clause's query-results? 我怎么知道一个子句的查询结果包含另一个子句的查询结果? For example: 例如:

"id>0" contains "id>1"
"id>0 or name='China'" contains "id>1"
"id>0 or name='China'" contains "name='China'"

Sorry,My English proficiency is very bad. 对不起,我的英语水平很差。

There is a table: Countries(id,name). 有一个表:countries(id,name)。

id      name
0       America
1       China
2       Japan
3       England

Obviously,The query-results of select id, name from Countries where id>0 contains select id, name from Countries id>2 's. 显然, select id, name from Countries where id>0的查询结果包含select id, name from Countries id>2select id, name from Countries id>2 I want to get the result by comparing the two where-clauses directly, I don't want to execute the actual query operation 我想通过直接比较两个where子句来获得结果,我不想执行实际的查询操作

I use java. 我使用Java。

It depends on your data, so you have to run each where clauses by joining their results. 这取决于您的数据,因此您必须通过联接它们的结果来运行每个where子句。 You can join them and check if any result exists. 您可以加入他们并检查是否存在任何结果。

You can use set operators to compare the two. 您可以使用集合运算符来比较两者。 If query2 does not have any rows that aren't in query1, than query1 contains query2. 如果query2没有query1中没有的任何行,则query1包含query2。

Eg, for Oracle: 例如,对于Oracle:

SELECT *
FROM   my_table
WHERE  condtion2
MINUS
SELECT *
FROM   my_table
WHERE  condtion1

For MS SQL Server and PostgreSQL, use EXCEPT instead of MINUS . 对于MS SQL Server和PostgreSQL,请使用EXCEPT而不是MINUS

Well, first of all, you'd need a way to represent your condition other than just text. 好吧,首先,你需要一种方法来代表你的其他条件的不仅仅是文本。 Without fleshing out all of the classes, below should get the gist of it. 在不充实所有课程的前提下,下面应有其要点。

public abstract class Condition implements Comparable<C extends Condition> {
    public abstract boolean equals(C other);
    public abstract boolean contains(C other);
};

public class OrCondition extends Condition {
    // contains a list of the conditions it's OR-ring together

    public boolean contains(Condition other) {
        // true if the list of conditions 
        // contains a condition c such that c.equals(other)
    }
}

public class GreaterThanCondition extends Condition {
    // contains a fieldname and a number to compare it to

    public boolean contains(Condition other) {
        // true if other is a GreaterThanCondition as well
        // on the same field and the number other.number >= this.number
    }
}

And so on. 等等。 Comparing strings and coming up with other conditions is left as an exercise to the OP (or the reader, if the reader is inclined to do so). 比较字符串并提出其他条件作为OP(或读者,如果读者倾向于这样做)的练习。


Of course, this works up to a point. 当然,这可以起到一定作用。 As soon as you want to answer questions that depend on the data, you have no choice but to execute some query. 一旦您想回答依赖于数据的问题,您别无选择,只能执行一些查询。 For instance, there is easier way to find out that WHERE id > 1 contains WHERE name > 'China' than issueing the queries. 例如,与发出WHERE id > 1 ,发现WHERE id > 1包含WHERE name > 'China'方法要容易得多。

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

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