You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

MariaDB查询异常:符合条件的部分记录未被选中(跨设备不一致)

MariaDB查询漏选记录的异常问题

运行以下查询语句时出现异常:该语句原本用于筛选dischargedate为NULL或大于当前日期前一天的员工,此前运行正常,但现在部分符合条件的记录被漏选。

WITH mt AS (SELECT id, MAX(id), employee_id FROM hirings group BY employee_id)
SELECT d.*, mt.employee_id, mt.id FROM mt
LEFT JOIN hirings a ON a.id = mt.id
LEFT JOIN employees d ON d.id = mt.employee_id
WHERE (dischargedate IS NULL OR dischargedate > CURDATE()-1) AND company_id = 1
GROUP BY d.id ORDER BY surname

诡异的是,在相同数据库、相同查询语句、相同MariaDB版本、相同Windows 10 Professional系统环境下,只有部分PC出现该问题,其他PC运行正常。手动对比employees和hirings表的记录,数据完全一致,无法定位问题原因。

另外,将相同数据库导入测试环境后未出现该问题,而出现问题的PC在无任何软件修改的情况下,约两个月前开始出现此异常。


漏选记录示例(员工DOE)

执行查询:SELECT * FROM employees WHERE surname='DOE'
结果:employees (1r x 35)

------------------------------------
| id | Surname | Name | Address | …
|----|---------|------|---------|---
| 413|   DOE   | JOHN | xxx     | …
|----|---------|------|---------|---

执行查询:SELECT * FROM hirings WHERE employee_id = 413
结果:hirings (5r x 11)

-----------------------------------------------------------------------------------------------------------------------------------------
| id | Employee_id | WeekHour | Level_id | Contract_id | DeadlineType | DeadlineDate | PartTime | ChargeDate | DischargeDate | Contract |
|----|-------------|----------|----------|-------------|--------------|--------------|----------|------------|---------------|----------|
| 493|     413     |   40     |     4    |      4      |        1     | 2022-01-31   |    0     | 2021-12-20 | 2022-01-31    |  (NULL)  |
| 545|     413     |   40     |     4    |      4      |        1     | 2022-04-30   |    0     | 2022-02-01 | 2022-04-30    |  (NULL)  |
| 618|     413     |   40     |     4    |      4      |        1     | 2022-06-30   |    0     | 2022-05-01 | 2022-06-30    |  (NULL)  |
| 661|     413     |   40     |     4    |      2      |        4     | 2022-12-31   |    0     | 2022-07-01 | 2022-10-07    |  (NULL)  |
| 746|     413     |   40     |     4    |      1      |        1     | (NULL)       |    0     | 2022-10-10 | (NULL)        |  (NULL)  |
|----|-------------|----------|----------|-------------|--------------|--------------|----------|------------|---------------|----------|

正常显示记录示例(员工STOJ)

执行查询:SELECT * FROM employees WHERE surname='STOJ'
结果:employees (1r x 35)

------------------------------------
| id | Surname | Name | Address | …
------------------------------------
| 469|  STOJ   | MARY | xxx     | …
------------------------------------

执行查询:SELECT * FROM hirings WHERE employee_id = 469
结果:hirings (3r x 11)

-----------------------------------------------------------------------------------------------------------------------------------------
| id | Employee_id | WeekHour | Level_id | Contract_id | DeadlineType | DeadlineDate | PartTime | ChargeDate | DischargeDate | Contract |
-----------------------------------------------------------------------------------------------------------------------------------------
| 629|     469     |   36     |     9    |      3      |        4     | 2022-10-31   |    0     | 2022-05-09 | 2022-10-31    |  (NULL)  |
| 761|     469     |   36     |     2    |      2      |        1     | 2023-03-31   |    0     | 2022-11-01 | 2023-03-31    |  (NULL)  |
| 943|     469     |   36     |     2    |      2      |        1     | 2023-10-30   |    0     | 2023-04-01 | (NULL)        |  (NULL)  |
-----------------------------------------------------------------------------------------------------------------------------------------

内容的提问来源于stack exchange,提问作者Stefano

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 05:35:43