运行查询的 MS Access 帮助

Posted

技术标签:

【中文标题】运行查询的 MS Access 帮助【英文标题】:MS Access Help to run a query 【发布时间】:2019-01-24 10:25:15 【问题描述】:

我有两张表如下。

我想获取所有活跃的会员,付款期=每两周一次,以及每个会员在特定年份和月份的相应储蓄记录。请注意,某些成员可能没有给定年份和月份的储蓄记录。 我写了一个查询,但它没有返回在储蓄表中没有储蓄记录的成员。

SELECT Member.memberID As [ID],Member.firstName AS [First Name],
       Member.lastName AS [Last Name], Member.paymentPeriod AS [Payment Period],
       Savings.savingId AS [Savings Id], Savings.enteredAmount AS [Amount]
FROM Member
    LEFT JOIN Savings ON Member.memberID = Savings.memberId
WHERE Member.isActive = 'Active' AND Member.paymentPeriod = 'Fortnightly'
      AND Year(Savings.enteredDate)=2019 AND Month(Savings.enteredDate) = 1;

【问题讨论】:

【参考方案1】:

SAVINGS 条件从WHERE 移动到ON 以获得真正的LEFT JOIN 结果:

SELECT Member.memberID As [ID],Member.firstName AS [First Name],
       Member.lastName AS [Last Name], Member.paymentPeriod AS [Payment Period],
       Savings.savingId AS [Savings Id], Savings.enteredAmount AS [Amount]
FROM Member
    LEFT JOIN Savings ON Member.memberID = Savings.memberId
      AND Year(Savings.enteredDate)=2019 AND Month(Savings.enteredDate) = 1
WHERE Member.isActive = 'Active' AND Member.paymentPeriod = 'Fortnightly';

(使用WHERE 子句中的这些条件,您将获得常规的INNER JOIN 结果。)

编辑:尝试避免“Join expression not supported error”:

SELECT Member.memberID As [ID],Member.firstName AS [First Name],
       Member.lastName AS [Last Name], Member.paymentPeriod AS [Payment Period],
       Savings.savingId AS [Savings Id], Savings.enteredAmount AS [Amount]
FROM Member
    LEFT JOIN Savings ON Member.memberID = Savings.memberId
WHERE Member.isActive = 'Active' AND Member.paymentPeriod = 'Fortnightly'
      AND (Year(Savings.enteredDate)=2019 or Year(Savings.enteredDate) IS NULL)
      AND (Month(Savings.enteredDate) = 1 or Month(Savings.enteredDate) IS NULL);

【讨论】:

我试过了,但我得到了 Join expression not supported 错误:(

以上是关于运行查询的 MS Access 帮助的主要内容,如果未能解决你的问题,请参考以下文章

MS access 2010 SQL查询帮助

带有子查询的 MS ACCESS 查询

需要帮助在 MS Access 中实施完全外部联接

不断增长的 MS Access 文件大小问题

在 MS Access 中运行异步查询

不通过 C# 从 ms access 数据库返回任何数据,如果我在 ms access 上运行相同的查询,它将获取数据