SQL 查询未返回与其子查询结果所建议的值相同的值
SQL query not returning same value as its subqueries results would suggest
我编写了一个查询,用于查找本周有哪些学生登录了该应用程序。我的 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 + ")))";
它returns没什么。但是,如果我将其分成两个查询...
首先,此查询显示本周登录的所有用户:
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() + ") ";
此查询输出用户 ID 为 "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 + "))";
此查询输出用户 ID 为 "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))
尝试使用 "IN" 运算符而不是“=”来 select 多个结果而不是一个。
在您的代码中:
+ DBHelperTest.USER_ID + " IN (SELECT " + DBHelperTest.USER_ID
...
+ DBHelperTest.USER_ID + " IN (" + "SELECT "
嗯,很有趣。
我在下面写了这个格式很好的SQL(我已经将日期函数更改为我更熟悉的相应的 T-SQL 函数)。
我没有基础数据来玩这个查询,所以我只能看代码。
这看起来像是一系列独立的(不相关的)子查询,我想乍一看应该可行。
最深的子查询将 return 为 NULL 或单个值。
所有其他子查询都使用“=”并且等价运算符不能处理多个值,尽管查看子查询(没有数据)结果集中可能有多个值是可能的吗?
也许您只是没有从 SQLite 返回有意义的错误消息?
难道这就是问题所在?比在某处 returned 更有价值?
如果您使用“IN”而不是“=”,这可能会变得更加明显吗?
如果没有,您能否 post 一些样本数据,或许可以让问题重现?
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
)
)
)
)
我编写了一个查询,用于查找本周有哪些学生登录了该应用程序。我的 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 + ")))";
它returns没什么。但是,如果我将其分成两个查询...
首先,此查询显示本周登录的所有用户:
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() + ") ";
此查询输出用户 ID 为 "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 + "))";
此查询输出用户 ID 为 "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))
尝试使用 "IN" 运算符而不是“=”来 select 多个结果而不是一个。
在您的代码中:
+ DBHelperTest.USER_ID + " IN (SELECT " + DBHelperTest.USER_ID
...
+ DBHelperTest.USER_ID + " IN (" + "SELECT "
嗯,很有趣。 我在下面写了这个格式很好的SQL(我已经将日期函数更改为我更熟悉的相应的 T-SQL 函数)。
我没有基础数据来玩这个查询,所以我只能看代码。
这看起来像是一系列独立的(不相关的)子查询,我想乍一看应该可行。
最深的子查询将 return 为 NULL 或单个值。 所有其他子查询都使用“=”并且等价运算符不能处理多个值,尽管查看子查询(没有数据)结果集中可能有多个值是可能的吗? 也许您只是没有从 SQLite 返回有意义的错误消息?
难道这就是问题所在?比在某处 returned 更有价值? 如果您使用“IN”而不是“=”,这可能会变得更加明显吗?
如果没有,您能否 post 一些样本数据,或许可以让问题重现?
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
)
)
)
)