我正试图在一个遗留系统(运行MySQL4.x)上使用NOT EXISTS
MySQL函数运行一个MySQL查询,下面是一个简单的查询,它从两个表中返回几列(带有一个内部联接).
Nb customerId
和accountId
是相同的值.这是一个非常古老的制度:
SELECT d.customerId, d.customerName, a.status
FROM DetailedDebtorsData AS d
INNER JOIN accounts AS a ON a.account_uid = d.customerId
ORDER BY d.date ASC;
+---------------+----------------------+--------+
| customerId | customerName | status |
+---------------+----------------------+--------+
| 145060 | Alan Smith | active |
| 68742 | John Doe | active |
+---------------+----------------------+--------+
我还有另一个表,我将用作‘Exceptions’表,实际上,如果在account_payment_terms
个Exceptions表中找到了CustomerID(及其在排除中的相关Account_id列),那么我不想从上面的DetailedDebtors表中返回相关行.
account_payment_terms
表(不包括在内)
mysql> SELECT account_id, created_date FROM account_payment_terms;
+------------+---------------------+
| account_id | created_date |
+------------+---------------------+
| 145060 | 2023-11-20 13:23:03 |
+------------+---------------------+
因为Account_id145060
存在于accountPaymentTerms
表中,所以我不想返回DetailedDebtors
表中要返回的相关行(具有相同的145060 id).
Here is the query I am trying to execute with NOT EXISTS()个
SELECT d.customerId, d.customerName, a.status
FROM DetailedDebtorsData AS d
INNER JOIN accounts AS a ON a.account_uid = d.customerId
WHERE NOT EXISTS (
SELECT 1
FROM account_payment_terms AS apt
WHERE apt.account_id = d.customerId
)
ORDER BY d.date ASC;
这会出错,并给出以下错误,但我希望WHERE NOT EXISTS只返回一行,即不在account_payment_terms
表中的行.
MySQL返回错误
ERROR 1064 (HY000): You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'EXISTS (
SELECT 1
FROM account_payment_terms AS
预期结果(返回1行)
+---------------+----------------------+--------+
| accountId | customerName | status |
+---------------+----------------------+--------+
| 68742 | John Doe | active |
+---------------+----------------------+--------+
实际结果(返回2行)
+---------------+----------------------+--------+
| accountId | customerName | status |
+---------------+----------------------+--------+
| 145060 | Alan Smith | active |
| 68742 | John Doe | active |
+---------------+----------------------+--------+
我做错了什么?