MariaDB查询结果错误

huangapple go评论58阅读模式
英文:

MariaDB Wrong Query result

问题

我有一个关于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大于今天日期的员工。

直到最近,这个查询一直正常工作,但现在开始不选择一些应该选择的记录。

但在某些电脑上出现了这个问题,而在其他具有相同数据库和相同查询的电脑上一切正常。

所有电脑都运行Windows 10 Professional,MariaDB版本相同。

我手动提取了员工表和招聘表的记录以与两个数据库进行比较。
提取出的记录是相同的,我找不到这个问题的解释。

示例:
--> 此记录不会出现在查询结果中:

查询: SELECT * FROM employees WHERE surname='DOE'

结果: employees (1行 x 35)

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

查询: SELECT * FROM hirings WHERE employee_id = 413

结果: hirings (5行 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'**

结果: **employees (1行 x 35)**


| id | Surname | Name | Address | …

| 469| STOJ | MARY | xxx | …


查询: **SELECT * FROM hirings WHERE employee_id = 469**

结果: **hirings (3行 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上,这个问题在大约两个月前出现,没有进行任何软件修改。

英文:

I've a strange problem with a Query on 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

The query select all employees that have dischargedate valorized to NULL or dischargedate valorized with a value greater then today's date.

Until recently this query works fine, now it have started to doesn't select some records that should be selected.

But in some PC happens this and in other PC whith same DB and same query all works fine.

All PC's have Windows 10 Professional and the MariaDB version is the same.

I extracted manually (with a query) the record of employees and hirings table for comparation with the 2 databases.
The record extraced are identical and I can't find a explanation for this issue.

Example:
--> This record does not appear in the query result:

Query: SELECT * FROM employees WHERE surname=’DOE’

Result: employees (1r x 35)

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

Query: SELECT * FROM hirings WHERE employee_id = 413

Result: 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)  |
|----|-------------|----------|----------|-------------|--------------|--------------|----------|------------|---------------|----------|

--> This record appear in the query result:
Query: SELECT * FROM employees WHERE surname=’STOJ’

Result: employees (1r x 35)

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

Query: SELECT * FROM hirings WHERE employee_id = 469

Result: 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)  |
-----------------------------------------------------------------------------------------------------------------------------------------

I want to emphasize that this issue doesn't appears on my PC with the same database imported for tests.
In other PC's this issue was appears about two month ago without any software modification.

答案1

得分: 1

看起来你的意思是要执行 SELECT MAX(id) as id, employee_id,而不是 SELECT id, MAX(id), employee_id。后者会为每个 employee_id 返回一个任意的 id 值(并生成一个名为 MAX(id) 的额外列,你并没有使用它)。

启用 ONLY_FULL_GROUP_BY SQL 模式将防止你犯类似的错误。这在最近的 MySQL 中是默认设置的。

英文:

Looks like you mean to be doing SELECT MAX(id) as id, employee_id, not SELECT id, MAX(id), employee_id. The latter will return an arbitrary one of the id values for each employee_id (and generates an mt column named MAX(id) which you don't use).

Enabling the ONLY_FULL_GROUP_BY sql mode will prevent you from making mistakes like this. This is the default in recent mysql.

答案2

得分: 0

我已解决,用以下内容替换了旧查询:

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
英文:

I solved replacing the old query with this:

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

huangapple
  • 本文由 发表于 2023年6月9日 02:21:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76434690.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定