简体   繁体   中英

time difference calculation in sql

Hello I'm trying to create a trigger(or more) in order to calculate the time difference in a mysql table (I'm using mysql 5.7.21). The table I have is this :

CREATE TABLE people(
username VARCHAR(255) NOT NULL,
state VARCHAR(255) NOT NULL DEFAULT 'not active',
PRIMARY KEY(username)
);

Some explanation about the table : The username column is self-explanatory and the state column get only 1 out of 3 values : 'active', 'not active','working' . Specifically a person can change from 'active' to 'not active'/'working' and vice-versa but cannot change from 'not active' to 'working' or from 'working' to 'not active'. What I want to do is to keep track of how much time has a person been active/working for. Now my idea is have a table like this :

Create table Time(
username VARCHAR(255) NOT NULL,
timestarted TIME,
timeended TIME,
timeworking TIME,
FOREIGN KEY (username) REFERENCES people(username)
);

My first guess was to use the CURRENT_TIME() function to keep track of state using triggers. What I did is :

Delimiter $$

CREATE TRIGGER time_calculation
BEFORE Update 
ON people 
FOR EACH ROW BEGIN
DECLARE @times time;
DECLARE @timee time;
DECLARE @timet time;
IF OLD.state = 'not active' THEN
UPDATE Time SET timestart = CURRENT_TIME()  WHERE username = new.username;
END IF;
IF NEW.state = 'not active' THEN
set @times = (select timestarted from time where username = new.username);
set @timee = (select timeended from time where username = new.username);
set @timet = (select timeworking from time where username = new.username);
    UPDATE Time SET timeend = CURRENT_TIME(),timeworking = (TIMEDIFF(times,timee)+timet) WHERE username = new.username;
    END IF;
END$$ 

According to this trigger. Every time someone switches from 'not active' to 'active' the Time table will get the current time of that switch as timestart.When someone switches to 'not active' the Time table will get the current time as timeend and it will add the difference between timestart and timeend to timeworking. The trigger shows the following error :

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DECLARE @times time;
DECLARE @timee time;
DECLARE @timet time;
BEGIN
IF OLD.state =' at line 5

Don't know what's wrong with it. Any ideas?

Well I found a way to solve this problem although it wasn't as I expected it. I made some changes in the table structure as follows :

Create table people (
username VARCHAR(255) NOT NULL,
Primary Key (username)
);

Create table characteristics (
username VARCHAR(255) NOT NULL,
state VARCHAR(25) NOT NULL DEFAULT 'not active',
timestart TIME,
timestop TIME,
timetotal TIME NOT NULL DEFAULT '00:00:00',
FOREIGN KEY(username) REFERENCES people(username)
);

As for the trigger to get the time difference it's as follows :

Delimiter $$

CREATE TRIGGER time_calculation
BEFORE Update 
ON characteristics 
FOR EACH ROW BEGIN
IF OLD.state = 'not active' && NEW.state != 'not active' THEN
SET NEW.timestart = CURRENT_TIME();
END IF;
IF NEW.state = 'not active' && OLD.state != 'not active' THEN
SET NEW.timestop = CURRENT_TIME();
SET NEW.timetotal = ADDTIME(OLD.timetotal,TIMEDIFF(NEW.timestop, OLD.timestart));
END IF;
END$$ 

Delimiter ;

Hope this helps anyone having the same problem with me !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM