SQL查询没有返回与其子查询结果相同的值

时间:2015-01-05 14:15:46

标签: android sql sqlite

我已经编写了一个查询,该查询应该可以找到哪些学生在本周登录了应用程序。我的SQLite查询如下所示:

public static final String WHOLE_QUERY = "SELECT " + DBHelperTest.USER_ID
            + " , " + DBHelperTest.FNAME + " , " + DBHelperTest.SNAME
            + " FROM " + DBHelperTest.TABLE_CHILD + " WHERE "
            + DBHelperTest.USER_ID + " = (SELECT " + DBHelperTest.USER_ID
            + " FROM " + DBHelperTest.TABLE_SESSION_DATA + " WHERE "
            + DBHelperTest.WEEK_OF_YEAR_SIGNED_IN + " = " + theWeek() + " AND "
            + DBHelperTest.YEAR_SIGNED_IN + " = " + theYear() + ") " + " AND "
            + DBHelperTest.USER_ID + " = (" + "SELECT "
            + DBHelperTest.Child_USER_ID + " FROM " + DBHelperTest.TABLE_CLASS
            + " WHERE " + DBHelperTest.Teacher_USER_ID + " = (SELECT "
            + DBHelperTest.USER_ID + " FROM " + DBHelperTest.TABLE_SESSION_DATA
            + " WHERE " + DBHelperTest.SESSION_ID + " = ( SELECT MAX("
            + DBHelperTest.SESSION_ID + ") FROM "
            + DBHelperTest.TABLE_SESSION_DATA + ")))";

它什么都不返回。但是如果我把它分成两个查询...

首先,此查询显示本周登录的所有用户:

public static final String LOGGED_IN_THIS_WEEK_QUERY = "SELECT "
            + DBHelperTest.USER_ID + " , " + DBHelperTest.FNAME + " , "
            + DBHelperTest.SNAME + " FROM " + DBHelperTest.TABLE_CHILD
            + " WHERE " + DBHelperTest.USER_ID + " = (SELECT "
            + DBHelperTest.USER_ID + " FROM " + DBHelperTest.TABLE_SESSION_DATA
            + " WHERE " + DBHelperTest.WEEK_OF_YEAR_SIGNED_IN + " = "
            + theWeek() + " AND " + DBHelperTest.YEAR_SIGNED_IN + " = "
            + theYear() + ") ";

此查询使用userId“Jasm”输出用户。

其次,此查询显示与给定教师关联的所有用户:

   public static final String CHILD_WITH_SAME_TEACHER_QUERY = "SELECT "
                + DBHelperTest.Child_USER_ID + " FROM " + DBHelperTest.TABLE_CLASS
                + " WHERE " + DBHelperTest.Teacher_USER_ID + " = (SELECT "
                + DBHelperTest.USER_ID + " FROM " + DBHelperTest.TABLE_SESSION_DATA
                + " WHERE " + DBHelperTest.SESSION_ID + " = ( SELECT MAX("
                + DBHelperTest.SESSION_ID + ") FROM "
                + DBHelperTest.TABLE_SESSION_DATA + "))";

此查询使用userId的“Jasm”和“thup”输出用户。

为什么我的原始查询不输出“Jasm”的详细信息,因为此用户符合这两个标准?

请参阅下面的更易阅读的版本:

整个查询:

SELECT USER_ID ,  FNAME , SNAME 
FROM TABLE_CHILD
WHERE USER_ID = 
(
SELECT USER_ID  
FROM TABLE_SESSION_DATA
WHERE WEEK_OF_YEAR_SIGNED_IN = theWeek()  
AND YEAR_SIGNED_IN = theYear()
) 
AND USER_ID = 
(
SELECT Child_USER_ID 
FROM TABLE_CLASS
WHERE Teacher_USER_ID = 
(
SELECT USER_ID 
FROM TABLE_SESSION_DATA
WHERE SESSION_ID = 
( 
SELECT MAX(SESSION_ID) 
FROM TABLE_SESSION_DATA)))

上半场

SELECT USER_ID ,  FNAME , SNAME 
FROM TABLE_CHILD
WHERE USER_ID = 
(
SELECT USER_ID  
FROM TABLE_SESSION_DATA
WHERE WEEK_OF_YEAR_SIGNED_IN = theWeek()  
AND YEAR_SIGNED_IN = theYear()
) 

下半场

SELECT Child_USER_ID 
FROM TABLE_CLASS
WHERE Teacher_USER_ID = 
(
SELECT USER_ID 
FROM TABLE_SESSION_DATA
WHERE SESSION_ID = 
( 
SELECT MAX(SESSION_ID) 
FROM TABLE_SESSION_DATA))

2 个答案:

答案 0 :(得分:0)

尝试使用" IN"运算符而不是" ="选择多个结果而不只是一个。

在您的代码中:

+ DBHelperTest.USER_ID + " IN (SELECT " + DBHelperTest.USER_ID
...
+ DBHelperTest.USER_ID + " IN (" + "SELECT "

答案 1 :(得分:0)

嗯很有意思。 我在下面写了这个格式很好的SQL(我已经将日期函数更改为我更熟悉的相应T-SQL函数)。

我没有底层数据允许我使用此查询,因此我只能查看代码。

这看起来像是一系列自包含(非相关)子查询,我猜乍一看应该有效。

最深的子查询将返回NULL或单个值。 所有其他子查询都使用“=”而等价运算符不能使用多个值,虽然查看子查询(没有数据)结果集中可能有多个值? 也许你只是没有从SQLite那里得到有意义的错误消息?

这可能是它出错的地方吗?某些地方还有更多的价值回归? 如果使用“IN”而不是“=”,这可能会更明显吗?

如果没有,您是否可以发布一些样本数据,以便重复问题?

SELECT USER_ID, FNAME .SNAME
FROM TABLE_CHILD
WHERE USER_ID =
    (
        SELECT USER_ID
        FROM TABLE_SESSION_DATA
        WHERE WEEK_OF_YEAR_SIGNED_IN  = datepart(week,getdate())
        AND
        YEAR_SIGNED_IN  =  datepart(year,getdate()) 
        AND
        USER_ID =
             (
                SELECT Child_USER_ID
                FROM TABLE_CLASS
                WHERE Teacher_USER_ID =
                        (
                            SELECT USER_ID
                            FROM
                            TABLE_SESSION_DATA
                            WHERE SESSION_ID = 
                                (
                                    SELECT MAX(SESSION_ID)
                                    FROM TABLE_SESSION_DATA
                                )
                         )
              )
    )