Bug #108143 TIMESTAMPDIFF function dones not work as expect on where caluse

Submitted: 16 Aug 2022 5:02 Modified: 14 Jul 14:25
Reporter: zhou bing Email Updates:
Status: Closed Impact on me:
None 
Category:MySQL Server: DML Severity:S2 (Serious)
Version:5.7 OS:Any
Assigned to: CPU Architecture:Any

[16 Aug 2022 5:02] zhou bing
Description:
TIMESTAMPDIFF function dones not work as expect on `WHERE` caluse where there are two calling with different unit parameter and same datetime expr parameters. 

It seems that two TIMESTAMPDIFF fucntion calls with same datetime expr parameters on `where` clause will return same result although they have different unit parameter.

fucntion calls something like this:

  TIMESTAMPDIFF(DAY,u.dtime, '2022-08-16 04:30:58') 
  TIMESTAMPDIFF(MINUTE, u.dtime,'2022-08-16 04:30:58')

These two function call have differe result on `SELECT` clause and same result on  `WHERE` clause.

How to repeat:
create table and insert data:

```SQL
CREATE TABLE T_TIMESTAMPDIFF (id int NOT NULL AUTO_INCREMENT, dtime datetime, PRIMARY KEY (id)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ;
INSERT INTO T_TIMESTAMPDIFF (id, dtime) VALUES (1, '2022-08-03 10:37:33');
```

### query:

SELECT
  TIMESTAMPDIFF(DAY, u.dtime, '2022-08-16 04:30:58')    diffDay,
  TIMESTAMPDIFF(MINUTE, u.dtime, '2022-08-16 04:30:58') diffMin
FROM
  T_TIMESTAMPDIFF u
WHERE id = 1

#### get result:

diffDay	diffMin
12	18353

### query:
SELECT
  TIMESTAMPDIFF(DAY, u.dtime, '2022-08-16 04:30:58')    diffDay,
  TIMESTAMPDIFF(MINUTE, u.dtime, '2022-08-16 04:30:58') diffMin
FROM
  T_TIMESTAMPDIFF u
WHERE id = 1
AND TIMESTAMPDIFF(DAY,u.dtime, '2022-08-16 04:30:58') = 12
-- AND TIMESTAMPDIFF(MINUTE, u.dtime,'2022-08-16 04:30:58') = 18353;

or

SELECT
  TIMESTAMPDIFF(DAY, u.dtime, '2022-08-16 04:30:58')    diffDay,
  TIMESTAMPDIFF(MINUTE, u.dtime, '2022-08-16 04:30:58') diffMin
FROM
  T_TIMESTAMPDIFF u
WHERE id = 1
-- AND TIMESTAMPDIFF(DAY,u.dtime, '2022-08-16 04:30:58') = 12
AND TIMESTAMPDIFF(MINUTE, u.dtime,'2022-08-16 04:30:58') = 18353;

#### get result:

diffDay	diffMin
12	18353

### query:

SELECT
  TIMESTAMPDIFF(DAY, u.dtime, '2022-08-16 04:30:58')    diffDay,
  TIMESTAMPDIFF(MINUTE, u.dtime, '2022-08-16 04:30:58') diffMin
FROM
  T_TIMESTAMPDIFF u
WHERE id = 1
AND TIMESTAMPDIFF(DAY,u.dtime, '2022-08-16 04:30:58') = 12
AND TIMESTAMPDIFF(MINUTE, u.dtime,'2022-08-16 04:30:58') = 18353;

#### get empty result

when I try query:

SELECT
  TIMESTAMPDIFF(DAY, u.dtime, '2022-08-16 04:30:58')    diffDay,
  TIMESTAMPDIFF(MINUTE, u.dtime, '2022-08-16 04:30:58') diffMin
FROM
  T_TIMESTAMPDIFF u
WHERE id = 1
AND TIMESTAMPDIFF(DAY,u.dtime, '2022-08-16 04:30:58') = 12
AND TIMESTAMPDIFF(MINUTE, u.dtime,'2022-08-16 04:30:58') = 12;

#### get result:

diffDay	diffMin
12	18353
[16 Aug 2022 5:03] zhou bing
fix version
[16 Aug 2022 13:07] MySQL Verification Team
Hi Mr. bing,

Thank you for your bug report.

We have ran a test case and got the same result as you with 8.0.30.

We agree with you on the severity of this bug.

Verified as reported.
[28 May 1:13] MySQL Admin
Posted by developer: Bug status updated to 'Documenting'
[14 Jul 14:25] Edward Gilmore
Posted by developer:
 
Added the following note to the MySQL Server 9.7.2 and 26.7.0 release notes:

TIMESTAMPDIFF() returned unexpected results when used with a WHERE clause.

Portions of this website are copyright © 2001, 2002 The PHP Group

Page generated in 0.054 sec. using MySQL 8.4.7-u6-cloud

Timestamp references displayed by the system are UTC.
Content reproduced on this site is the property of the respective copyright holders.
It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.