SQLite选择何时值不在另一个表上

时间:2014-04-24 14:37:37

标签: java sqlite

我有一个可以添加到活动中的玩家列表。 我想选择一个尚未列入活动列表的玩家列表。 我的查询是:

    stmt.executeQuery("SELECT PLAYER_NO,FIRST_NAME,SURNAME,"
                + "EMAIL,PHONE_NO FROM PLAYER"
                + "WHERE NOT EXISTS(SELECT PLAYER_NO FROM EVENT_PLAYER_LIST "
                + "WHERE EVENT_NO = "+eventKey+" & "
                + "EVENT_PLAYER_LIST.PLAYER_NO = PLAYER.PLAYER_NO);");

有人能发现问题或告诉我该怎么做吗?

以下是两个演变的表格:

    String sql = " CREATE TABLE IF NOT EXISTS EVENT_PLAYER_LIST"
                    + " (EVENT_NO       INT     NOT NULL,"
                    + " PLAYER_NO       INT     NOT NULL,"
                    + " ACTIVE          INT,"
                    + " TIME_ENROLLED   CHAR(50),"
                    + " WINS            INT,"
                    + " LOSES           INT,"
                    + " DRAWS           INT,"
                    + " PRIMARY KEY (EVENT_NO, PLAYER_NO))";


    String sql = " CREATE TABLE IF NOT EXISTS PLAYER"
                    + " (PLAYER_NO       INTEGER     PRIMARY KEY    AUTOINCREMENT      NOT NULL,"
                    + " FIRST_NAME      CHAR(50)    NOT NULL,"
                    + " SURNAME         CHAR(50)    NOT NULL,"
                    + " DOB             CHAR(50),"
                    + " CLUB            CHAR(50),"
                    + " EMAIL           CHAR(120),"
                    + " PHONE_NO        CHAR(50))";

如果我有几个玩家加入了一个活动,他们仍然会出现在除第一个玩家之外的玩家名单上。

1 个答案:

答案 0 :(得分:1)

我认为你的查询应该是:

stmt.executeQuery("SELECT PLAYER_NO,FIRST_NAME,SURNAME,"
                + "EMAIL,PHONE_NO FROM PLAYER"
                + "WHERE NOT EXISTS(SELECT PLAYER_NO FROM EVENT_PLAYER_LIST "
                + "WHERE EVENT_NO = "+eventKey+" AND "
                + "EVENT_PLAYER_LIST.PLAYER_NO = PLAYER.PLAYER_NO);");

请注意使用 AND 代替 $ 。后者是Bitwise AND

请注意,构建查询的方式并不是最好的,通常建议您使用预准备语句(出于安全性和性能原因)