试图在我的 sql 中获得多个内部联接
Trying to get mutliple inner joins in my sql
我将 xampp 与 mysql 一起使用,并且一直在尝试格式化在 4 个单独的表上使用内部联接的查询。内部联接包含表 customer、bankaccounts、has 和 transactions。有 link 从 M:N 到 1:M 的银行账户和客户表,而交易是 linked 到银行账户。我尝试了以下查询:
SELECT t.TransactionID,
t.Description,
t.Amount,
t.isLoan,
t.TransactionDate,
a.AccountID,
a.SortCode,
a.Name,
a.CreationDate,
c.FirstName,
c.LastName
c.DateofBirth
FROM transactions AS t
INNER JOIN bankaccounts AS a
#inner join will create a new table by combining the values selected based on if they satisfy the on condition below
ON t.TransactionID IN ( SELECT t.TransactionID
FROM transactions
WHERE t.TransactionDate BETWEEN '2021-11-25' AND '2021-12-01'
)
AND t.AccountID = a.AccountID
INNER JOIN has ON has.AccountID = a.AccountID ---- > #multiple inner joins can occur on multiple tables
INNER JOIN customers AS c ON has.CustomerID = c.CustomerID;
然而,目前这只是一个错误。
我需要的是 link 所有表格,同时确保只选择那些特定日期之间的交易。
有什么办法可以解决吗?
您应该将带有子查询的条件移动到 WHERE
子句:
SELECT t.TransactionID, t.Description, t.Amount, t.isLoan, t.TransactionDate,
a.AccountID, a.SortCode, a.Name, a.CreationDate,
c.FirstName, c.LastName, c.DateofBirth
FROM transactions AS t
INNER JOIN bankaccounts AS a ON t.AccountID = a.AccountID
INNER JOIN has ON has.AccountID = a.AccountID
INNER JOIN customers AS c ON has.CustomerID = c.CustomerID
WHERE t.TransactionDate BETWEEN '2021-11-25' AND '2021-12-01';
我将 xampp 与 mysql 一起使用,并且一直在尝试格式化在 4 个单独的表上使用内部联接的查询。内部联接包含表 customer、bankaccounts、has 和 transactions。有 link 从 M:N 到 1:M 的银行账户和客户表,而交易是 linked 到银行账户。我尝试了以下查询:
SELECT t.TransactionID,
t.Description,
t.Amount,
t.isLoan,
t.TransactionDate,
a.AccountID,
a.SortCode,
a.Name,
a.CreationDate,
c.FirstName,
c.LastName
c.DateofBirth
FROM transactions AS t
INNER JOIN bankaccounts AS a
#inner join will create a new table by combining the values selected based on if they satisfy the on condition below
ON t.TransactionID IN ( SELECT t.TransactionID
FROM transactions
WHERE t.TransactionDate BETWEEN '2021-11-25' AND '2021-12-01'
)
AND t.AccountID = a.AccountID
INNER JOIN has ON has.AccountID = a.AccountID ---- > #multiple inner joins can occur on multiple tables
INNER JOIN customers AS c ON has.CustomerID = c.CustomerID;
然而,目前这只是一个错误。
我需要的是 link 所有表格,同时确保只选择那些特定日期之间的交易。
有什么办法可以解决吗?
您应该将带有子查询的条件移动到 WHERE
子句:
SELECT t.TransactionID, t.Description, t.Amount, t.isLoan, t.TransactionDate,
a.AccountID, a.SortCode, a.Name, a.CreationDate,
c.FirstName, c.LastName, c.DateofBirth
FROM transactions AS t
INNER JOIN bankaccounts AS a ON t.AccountID = a.AccountID
INNER JOIN has ON has.AccountID = a.AccountID
INNER JOIN customers AS c ON has.CustomerID = c.CustomerID
WHERE t.TransactionDate BETWEEN '2021-11-25' AND '2021-12-01';