我正试图在一个遗留系统(运行MySQL4.x)上使用NOT EXISTS MySQL函数运行一个MySQL查询,下面是一个简单的查询,它从两个表中返回几列(带有一个内部联接).

Nb customerIdaccountId是相同的值.这是一个非常古老的制度:

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 |
+---------------+----------------------+--------+

我做错了什么?

推荐答案

以下是执行排除联接MySQL 4.0的解决方案:

SELECT d.customerId, d.customerName, a.status
FROM DetailedDebtorsData AS d 
INNER JOIN accounts AS a ON a.account_uid = d.customerId
LEFT OUTER JOIN account_payment_terms AS apt
  ON apt.account_id = d.customerId
WHERE apt.account_id IS NULL
ORDER BY d.date ASC;

Mysql相关问答推荐

为什么我安装MySQL时不能使用3306端口?

根据计数按月和年对数据进行分组()

MySQL:一个查询中的SELECT语句,用于从一个表中检索所有数据,并仅从另一个表中检索一个数据

插入时发生日期时间字段溢出错误

MySQL Group by或Distinct in Window函数

MySQL grant语句中的用户名区分大小写

"Shelf服务器容器访问MySQL Docker容器时出现SocketException异常"

数据导入和默认目标架构空列表. (Mysql 工作台 8)

在 MySQL 中,如何对 LIKE 查询进行批量更新,删除字符?

Mysql 查询返回未定义的 node.js

在 MySQL 中跨日期和位置计算和排序实体的共现

在 MySQL 中使用非空条件 LAG() 函数

为什么order by子句可以利用索引?

判断一列中的多个值并返回值 1 或 0

使用数据表的直方图(SQL 查询)

终止空闲的mysql连接

MySQL - 基于同一表中的行求和列值

phpMyAdmin - 无法连接 - 无效设置 - 自从我添加了 root 密码 - 被锁定

什么是mysql的BETWEEN性能超过..?

设计用户角色和权限系统的最佳实践?