在我的 SQL 查询中使用 "or" 导致 MS Access 2019 崩溃
Using "or" in my SQL query crashes MS Access 2019
这是我在 Stack 上的第一个问题。
我是一名学生,试图通过练习培养我的提问能力。使用 Microsoft Access 和一些公开的人口普查数据(5 table 行,共 57,000 行)我遇到了以下问题。
当我尝试在“where”语句中使用“or”而不是“and”时,它导致 Access 停止响应。为什么会这样?
例如,当我尝试更改时:
not MARRIAGE.P_TOT_MARRD_REG_MARRGE = 0 and not EDUCATION.INFANTS_PRIMARY_TOT_P = 0
对此:
not MARRIAGE.P_TOT_MARRD_REG_MARRGE = 0 or not EDUCATION.INFANTS_PRIMARY_TOT_P = 0
甚至这样:
not MARRIAGE.P_TOT_MARRD_REG_MARRGE = 0 or EDUCATION.INFANTS_PRIMARY_TOT_P = 0
它会导致崩溃。
这是因为我的语法错误吗?还是我误解了Sequel中“or”子句的应用?
我没有使用“或”的完整代码如下。
(
select distinct top 10
CHILDREN.PK as top10Last10,
CHILDREN.TOTAL_TOTAL as children,
HOUSEHOLD.TOTAL_FAMHHOLD as familys,
FAMILY.INTCT_FAM_NO_OTR_CHLD_PRE_FAM as undivorced,
MARRIAGE.P_TOT_MARRD_REG_MARRGE as officiated,
EDUCATION.INFANTS_PRIMARY_TOT_P as primaryKto6
from
CHILDREN,
HOUSEHOLD,
FAMILY,
MARRIAGE,
EDUCATION
where
CHILDREN.PK = HOUSEHOLD.PK and
CHILDREN.PK = FAMILY.PK and
CHILDREN.PK = MARRIAGE.PK and
CHILDREN.PK = EDUCATION.PK and
not CHILDREN.TOTAL_TOTAL = 0 and
not HOUSEHOLD.TOTAL_FAMHHOLD = 0 and
not FAMILY.INTCT_FAM_NO_OTR_CHLD_PRE_FAM = 0 and
not MARRIAGE.P_TOT_MARRD_REG_MARRGE = 0 and
not EDUCATION.INFANTS_PRIMARY_TOT_P = 0
order by
CHILDREN.TOTAL_TOTAL asc,
HOUSEHOLD.TOTAL_FAMHHOLD asc,
FAMILY.INTCT_FAM_NO_OTR_CHLD_PRE_FAM asc,
MARRIAGE.P_TOT_MARRD_REG_MARRGE asc,
EDUCATION.INFANTS_PRIMARY_TOT_P asc
)
union all
(
select distinct top 10
CHILDREN.PK,
CHILDREN.TOTAL_TOTAL,
HOUSEHOLD.TOTAL_FAMHHOLD,
FAMILY.INTCT_FAM_NO_OTR_CHLD_PRE_FAM,
MARRIAGE.P_TOT_MARRD_REG_MARRGE,
EDUCATION.INFANTS_PRIMARY_TOT_P
from
CHILDREN,
HOUSEHOLD,
FAMILY,
MARRIAGE,
EDUCATION
where
CHILDREN.PK = HOUSEHOLD.PK and
CHILDREN.PK = FAMILY.PK and
CHILDREN.PK = MARRIAGE.PK and
CHILDREN.PK = EDUCATION.PK and
not CHILDREN.TOTAL_TOTAL = 0 and
not HOUSEHOLD.TOTAL_FAMHHOLD = 0 and
not FAMILY.INTCT_FAM_NO_OTR_CHLD_PRE_FAM = 0 and
not MARRIAGE.P_TOT_MARRD_REG_MARRGE = 0 and
not EDUCATION.INFANTS_PRIMARY_TOT_P = 0
order by
CHILDREN.TOTAL_TOTAL desc,
HOUSEHOLD.TOTAL_FAMHHOLD desc,
FAMILY.INTCT_FAM_NO_OTR_CHLD_PRE_FAM desc,
MARRIAGE.P_TOT_MARRD_REG_MARRGE desc,
EDUCATION.INFANTS_PRIMARY_TOT_P desc
);
此完整代码正在运行,returns table 我希望它能运行。
提前感谢您的任何回复。
首先,没有响应不是崩溃。
接下来,您可能需要更具体一些:
(not MARRIAGE.P_TOT_MARRD_REG_MARRGE = 0) or (not EDUCATION.INFANTS_PRIMARY_TOT_P = 0)
或:
not (MARRIAGE.P_TOT_MARRD_REG_MARRGE = 0 or EDUCATION.INFANTS_PRIMARY_TOT_P = 0)
此外,请确保您在这些字段上有索引。
这是我在 Stack 上的第一个问题。 我是一名学生,试图通过练习培养我的提问能力。使用 Microsoft Access 和一些公开的人口普查数据(5 table 行,共 57,000 行)我遇到了以下问题。
当我尝试在“where”语句中使用“or”而不是“and”时,它导致 Access 停止响应。为什么会这样?
例如,当我尝试更改时:
not MARRIAGE.P_TOT_MARRD_REG_MARRGE = 0 and not EDUCATION.INFANTS_PRIMARY_TOT_P = 0
对此:
not MARRIAGE.P_TOT_MARRD_REG_MARRGE = 0 or not EDUCATION.INFANTS_PRIMARY_TOT_P = 0
甚至这样:
not MARRIAGE.P_TOT_MARRD_REG_MARRGE = 0 or EDUCATION.INFANTS_PRIMARY_TOT_P = 0
它会导致崩溃。
这是因为我的语法错误吗?还是我误解了Sequel中“or”子句的应用?
我没有使用“或”的完整代码如下。
(
select distinct top 10
CHILDREN.PK as top10Last10,
CHILDREN.TOTAL_TOTAL as children,
HOUSEHOLD.TOTAL_FAMHHOLD as familys,
FAMILY.INTCT_FAM_NO_OTR_CHLD_PRE_FAM as undivorced,
MARRIAGE.P_TOT_MARRD_REG_MARRGE as officiated,
EDUCATION.INFANTS_PRIMARY_TOT_P as primaryKto6
from
CHILDREN,
HOUSEHOLD,
FAMILY,
MARRIAGE,
EDUCATION
where
CHILDREN.PK = HOUSEHOLD.PK and
CHILDREN.PK = FAMILY.PK and
CHILDREN.PK = MARRIAGE.PK and
CHILDREN.PK = EDUCATION.PK and
not CHILDREN.TOTAL_TOTAL = 0 and
not HOUSEHOLD.TOTAL_FAMHHOLD = 0 and
not FAMILY.INTCT_FAM_NO_OTR_CHLD_PRE_FAM = 0 and
not MARRIAGE.P_TOT_MARRD_REG_MARRGE = 0 and
not EDUCATION.INFANTS_PRIMARY_TOT_P = 0
order by
CHILDREN.TOTAL_TOTAL asc,
HOUSEHOLD.TOTAL_FAMHHOLD asc,
FAMILY.INTCT_FAM_NO_OTR_CHLD_PRE_FAM asc,
MARRIAGE.P_TOT_MARRD_REG_MARRGE asc,
EDUCATION.INFANTS_PRIMARY_TOT_P asc
)
union all
(
select distinct top 10
CHILDREN.PK,
CHILDREN.TOTAL_TOTAL,
HOUSEHOLD.TOTAL_FAMHHOLD,
FAMILY.INTCT_FAM_NO_OTR_CHLD_PRE_FAM,
MARRIAGE.P_TOT_MARRD_REG_MARRGE,
EDUCATION.INFANTS_PRIMARY_TOT_P
from
CHILDREN,
HOUSEHOLD,
FAMILY,
MARRIAGE,
EDUCATION
where
CHILDREN.PK = HOUSEHOLD.PK and
CHILDREN.PK = FAMILY.PK and
CHILDREN.PK = MARRIAGE.PK and
CHILDREN.PK = EDUCATION.PK and
not CHILDREN.TOTAL_TOTAL = 0 and
not HOUSEHOLD.TOTAL_FAMHHOLD = 0 and
not FAMILY.INTCT_FAM_NO_OTR_CHLD_PRE_FAM = 0 and
not MARRIAGE.P_TOT_MARRD_REG_MARRGE = 0 and
not EDUCATION.INFANTS_PRIMARY_TOT_P = 0
order by
CHILDREN.TOTAL_TOTAL desc,
HOUSEHOLD.TOTAL_FAMHHOLD desc,
FAMILY.INTCT_FAM_NO_OTR_CHLD_PRE_FAM desc,
MARRIAGE.P_TOT_MARRD_REG_MARRGE desc,
EDUCATION.INFANTS_PRIMARY_TOT_P desc
);
此完整代码正在运行,returns table 我希望它能运行。
提前感谢您的任何回复。
首先,没有响应不是崩溃。
接下来,您可能需要更具体一些:
(not MARRIAGE.P_TOT_MARRD_REG_MARRGE = 0) or (not EDUCATION.INFANTS_PRIMARY_TOT_P = 0)
或:
not (MARRIAGE.P_TOT_MARRD_REG_MARRGE = 0 or EDUCATION.INFANTS_PRIMARY_TOT_P = 0)
此外,请确保您在这些字段上有索引。