I have two tables
CREATE TABLE `contract` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`emp_id` int(11) DEFAULT NULL ,
`sign_time` datetime DEFAULT NULL ,
`end_time` datetime DEFAULT NULL ,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ;
CREATE TABLE `employee_detail` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(100) DEFAULT NULL ,
`stage` varchar(100) DEFAULT NULL ,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ;
and some data:
INSERT INTO `contract` (`id`, `emp_id`,`sign_time`, `end_time`) VALUES
('25', '83', '2018-11-21 00:00:00', '2018-12-01 15:27:00');
INSERT INTO `contract` (`id`, `emp_id`,`sign_time`, `end_time`) VALUES
('26', '94', '2018-11-21 00:00:00', '2018-12-01 15:23:00');
INSERT INTO `employee_detail` (`id`, `name`, `stage`) VALUES ('83', 'Michael',
'1');
INSERT INTO `employee_detail` (`id`, `name`, `stage`) VALUES ('94', 'John',
'1');
when I query the database with SQL:
SELECT
c.*
FROM
contract c
JOIN employee_detail e ON c.emp_id = e.id
WHERE
e.stage != - 1
AND (
TIMESTAMPDIFF(
MINUTE,
'2018-11-30 09:18:23',
c.end_time
)
) > 0
AND TIMESTAMPDIFF(
MONTH,
'2018-11-30 09:18:23',
c.end_time
) = 0
I got 0 records. But if I query with SQL:
SELECT
c.*
FROM
contract c
JOIN employee_detail e ON c.emp_id = e.id
WHERE
e.stage != - 1
AND (
TIMESTAMPDIFF(
MINUTE,
'2018-11-30 09:18:23',
c.end_time
)
) > '0'
AND TIMESTAMPDIFF(
MONTH,
'2018-11-30 09:18:23',
c.end_time
) = '0'
which turns interger 0 to string '0', I got two right records. I searched from https://dev.mysql.com/doc/refman/5.6/en/date-and-time-functions.html#function_timestampdiff, and I found that:
Returns datetime_expr2 − datetime_expr1, where datetime_expr1 and datetime_expr2 are date or datetime expressions. One expression may be a date and the other a datetime; a date value is treated as a datetime having the time part '00:00:00' where necessary. The unit for the result (an integer) is given by the unit argument. The legal values for unit are the same as those listed in the description of the TIMESTAMPADD() function.
I'm confused about the result and the explanation of Oracle. So timestampdiff function returns one integer value but when I use it in SQL statement I got an incorrect result while if I treated it as string value I got the right answer. Could anybody explain the weird phenomenon? Thanks a lot!