Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何知道where子句查询结果包含另一个where子句查询结果?_Java_Sql - Fatal编程技术网

Java 如何知道where子句查询结果包含另一个where子句查询结果?

Java 如何知道where子句查询结果包含另一个where子句查询结果?,java,sql,Java,Sql,如何知道where子句的查询结果包含另一where子句的查询结果? 例如: "id>0" contains "id>1" "id>0 or name='China'" contains "id>1" "id>0 or name='China'" contains "name='China'" 对不起,我的英语水平很差 有一个表:Countriesid,name id name 0 America 1 China 2 J

如何知道where子句的查询结果包含另一where子句的查询结果? 例如:

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

对不起,我的英语水平很差

有一个表:Countriesid,name

id      name
0       America
1       China
2       Japan
3       England
显然,id>0的select id,name from Countries的查询结果包含select id,name from Countries id>2。 我想通过直接比较两个where子句得到结果,我不想执行实际的查询操作


我使用java。

这取决于您的数据,因此您必须通过连接每个where子句的结果来运行它们。您可以加入它们并检查是否存在任何结果。

您可以使用集合运算符来比较两者。如果query2没有任何不在query1中的行,则query1包含query2

例如,对于Oracle:

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

对于MS SQL Server和PostgreSQL,请使用“除”而不是“减”。

首先,您需要一种方法来表示您的条件,而不仅仅是文本。在不充实所有课程的情况下,下面应该可以了解其要点

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
    }
}
等等。比较字符串和提出其他条件是留给OP或读者的练习,如果读者愿意这样做的话


当然,这在一定程度上是可行的。只要您想回答依赖于数据的问题,您就别无选择,只能执行一些查询。例如,找出WHERE id>1包含WHERE name>“China”比发出查询更容易。

有人理解这个问题吗??我想标记这一点,但我不确定;我不确定我是否理解这个问题。您是否询问是否可以执行一个使用WHERE子句的查询,然后再运行一个新查询,并在其上添加一个新WHERE子句?如果是这样的话,使用子查询就完全有可能做到这一点。i、 e.选择表A中的*FROM SELECT*,其中id=1,作为表1,其中国家='US',我的英语水平非常差。正是这样,只是OP想不运行查询就知道。非常感谢。是否存在实现条件接口的java库?我不知道。