MySQL Trigger - timestamp hour between? - mysql

I'm after a mysql trigger that will set the column peak to 0 or 1 based on the timestamp hour between 08:01 & 23:59.
Can anyone help?
example:-
userstats.timestamp = 2018-07-01 12:27:20
peak = 1

One option is to use the DATE_FORMAT function to isolate the time component of your timestamp, e.g.
SELECT *
FROM userstats
WHERE DATE_FORMAT(timestamp, '%H:%i') BETWEEN '08:01' AND '23:59';
Your actual query may not look exactly like this, but this would seem to answer the crux of your question.

Related

SQL query doesn't work on a specific day

I have a weird problem in mysql!
my query is
SELECT * FROM aa WHERE problemTime>= '2016/03/20' AND problemTime<= '2016/04/20'
the result of this query is nothing , but when I change the first time to 2016/03/19 or 2016/03/21 I have the following result! I mean these queries
SELECT * FROM aa WHERE problemTime>= '2016/03/21' AND problemTime<= '2016/04/20'
or
SELECT * FROM aa WHERE problemTime>= '2016/03/19' AND problemTime<= '2016/04/20'
the result in both time ( 19th and 21th) is
but when I use 20th the result is noting
my main table is
I change the format of time from 2016/03/20 to 2016-03-20 ( I mean change / to - ) but it doesn't have change too!
whats the problem?
You should really be running a query like this if your problemTime column is datetime type:
SELECT * FROM aa
WHERE
problemTime>= str_to_date('2016/03/20', '%Y/%m/%d') AND
problemTime <= str_to_date('2016/04/20', '%Y/%m/%d')
Don't rely on implicit conversions between string and date.. leave your table data alone and ensure you explicitly convert your where clause parameters to the same data type as in the table. Also remember that a date "without" a time is actually midnight on the day in question, and midnight is like zero, it's the first thing that happens on any given day. A time of 6am on a given date, is after midnight, so a query that asks for dates less than or equal to midnight on a particular date means the 6am date will be excluded
This is general good DB practice; do not convert table data where possible, because it can cause huge performance hits and wrong results
Your column "problemTime" have date with time. Do not convert table data, change your where clause (add time).
SELECT * FROM aa WHERE problemTime>= '2016/03/20 00:00:00' AND problemTime<= '2016/04/20 23:59:59'
Try this as per SQl Server.
SELECT * FROM aa
WHERe cast(problemTime AS date) between '2016/03/21' AND '2016/04/20'

MySQL add up time duration of entries [duplicate]

How to caculate sum of times of my colonne called "timeSpent" having this format: HH:mm
in SQL? I am using MySQL.
the type of my column is Time.
it has this structure
TimeFrom like 10:00:00 12:00:00 02:00:00
TimeUntil 08:00:00 09:15:00 01:15:00
Time spent
total time 03:15:00
SELECT SEC_TO_TIME( SUM( TIME_TO_SEC( `timeSpent` ) ) ) AS timeSum
FROM YourTableName
100% working code to get sum of time out of MYSQL Database:
SELECT
SEC_TO_TIME( SUM(time_to_sec(`db`.`tablename`)))
As timeSum
FROM
`tablename`
Try and confirm.
Thanks.
In MySQL, you would do something like this to get the time interval:
SELECT TIMEDIFF('08:00:00', '10:00:00');
Then to add the time intervals, you would do:
SELECT ADDTIME('01:00:00', '01:30:00');
Unfortunately, you're not storing dates or using 24-hour time, so these calculations would end up incorrect since your TimeUntil is actually lower than your TimeFrom.
Another approach would be (assuming you sort out the above issue) to store the time intervals as seconds using TIMESTAMPDIFF():
UPDATE my_table SET time_spent=TIMESTAMPDIFF(start, end));
SELECT SEC_TO_TIME(SUM(time_spent)) FROM my_table;
If the data type of the timeSpent column is TIME you should be able to use the following query:
SELECT SUM(timeSpent)
FROM YourTableName -- replace YourTableName with the actual table name
However, if that is not the case, you may have to use a cast to convert to the Time data type. Something like this should work:
SELECT SUM(timeSpent - CAST('0:0:0' as TIME))
FROM YourTableName -- replace YourTableName with the actual table name

MySQL query to search in between two time range between two dates using timestamp data

I have timestamp values in my db. It has values like 2014-11-25 10:30:00.
I need to get all records between two dates and that has time between certain range like between 2014-10-20 to 2014-11-25 and between 9am to 7pm..
I need the query for this...
You can use the following query , I used it in my code for displaying data between two dates.
SELECT * from tablename WHERE columnname BETWEEN '2014-10-20 00:00:00' AND '2014-11-25 23:59:59'
The query includes start time of the particular date to end time of ending particular date.
You edit your query according to your start and end timings.
You can use internal mysql functions for convert datetype.
I think you need DATE() and TIME() functions.
Details you can find here
Thanks for your reply guys. I have found the answer
SELECT * FROM alerts
WHERE DATE BETWEEN '2014-11-16' AND '2014-11-26'
AND TIME(DATE) BETWEEN '09:00' AND '19:00'
Is giving the expected result.. :-)

DateDiff function in MYSQL 5.1. Need Hours+Mins

MYSQL 5.1
I need to create a query that gives my result table three columns, match_date, match_start and match_duration
I need the match_duration column which takes match_start and match_end and displays how long each game has been on for in 00:00 hours format. So for example if it were 5hrs:30 mins it would be displayed 5:30 hours.
This is what I have so far and not sure where I'm going wrong:
SELECT match_date, match_start, DateDiff(hhmm, match_start, match_end) AS Duration
FROM MatchInfo;
Thanks
Try below:
TIME_FORMAT(TIMEDIFF(match_end, match_start), '%H:%i')
i.e.
SELECT match_date, match_start,
TIME_FORMAT(TIMEDIFF(match_end, match_start), '%H:%i') AS DURATION
FROM MatchInfo;

Select data based on timestamp and interval from mysql

In joomla, it has jos_session table in which time is stored as unixtimestamp in 'time' column.
I want to select the records which are older then a week or two days, basically any interval.
Not much hands on DB, but i did tried with date_sub, but it seems taking date as an argument. So I also tried using FROM_UNIXTIME to convert, but nothing seems to be working.
The last query I tried was
SELECT username FROM jos_session WHERE DATE_SUB(FROM_UNIXTIME(time,'Y-m-d'), INTERVAL 2 DAY );
But it seems to giving empty set and many warnings!
Can anyone please help!
Thanks in advance,
Tanmay
Try this. It should work:
SELECT
username
FROM
jos_session
WHERE
TO_DAYS(FROM_UNIXTIME(CAST(timeAS UNSIGNED))) < TO_DAYS(FROM_UNIXTIME(UNIX_TIMESTAMP()))-2