MariaDB错误查询结果

cetgtptt  于 2023-06-22  发布在  其他
关注(0)|答案(2)|浏览(155)

我在MariaDB上遇到了一个奇怪的问题。

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

查询将选择所有已将dischargedate值赋值为NULL或dischargedate值赋值为大于今天日期的员工。
直到最近,这个查询工作正常,现在它开始不选择一些应该选择的记录。
但在某些PC上会发生这种情况,而在其他PC上,相同的DB和相同的查询都可以正常工作。
所有PC都有Windows 10专业版,MariaDB版本是相同的。
我手动提取(查询)的雇员和招聘表的记录与2个数据库进行比较。提取的记录完全相同,我无法找到该问题的解释。

示例:-->查询结果中不显示该记录:

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

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

查询:SELECT * FROM hirings WHERE employee_id = 413
结果:雇用人数(5 r 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)  |
|----|-------------|----------|----------|-------------|--------------|--------------|----------|------------|---------------|----------|
  • -->查询结果中出现此记录:*

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

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

查询:SELECT * FROM hirings WHERE employee_id = 469
结果:雇用人数(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)  |
-----------------------------------------------------------------------------------------------------------------------------------------
  • 我想强调的是,这个问题不会出现在我的PC上,同时导入了相同的数据库进行测试。在其他PC上,这个问题大约在两个月前出现,没有任何软件修改。*
z3yyvxxp

z3yyvxxp1#

看起来你的意思是做SELECT MAX(id) as id, employee_id,而不是SELECT id, MAX(id), employee_id。后者将为每个employee_id返回任意一个id值(并生成一个名为MAX(id)的mt列,您不用它)。
启用ONLY_FULL_GROUP_BY sql模式将防止您犯这样的错误。这是最近mysql中的默认值。

qybjjes1

qybjjes12#

我解决了将旧查询替换为下面的问题:

SELECT * FROM employees d 
         WHERE EXISTS
         (SELECT 1 FROM hirings a 
          WHERE a.employee_id = d.id 
          AND (a.dischargedate IS NULL OR a.dischargedate > CURDATE()-1) 
          AND company_id = 1)
          GROUP BY d.id ORDER BY surname

相关问题