I have a cloclin program where studnets are clocked in and out. If a student is not clocked out by 6:30 PM at the end of the day I have a mysql statement that executes and clocks the student out at 6:30pm automatically to help keep the tables clean.
When a student is clocked into the system it fills the start time with the proper time and the end time is left as "0000-00-00 00:00:00". I am having a hard time determining the sql statement to detect the emtpty EndTime field to determine if it is blank. Since by default the table lists the blank date field as 0000-00-00 00:00:00" the CONCAT method I use to update the field seems to work when updating the field, but does not work to determine the criteria "WHERE"
My current sql statement is
$sql = "UPDATE `daycare attendance table` SET EndTime = CONCAT('1899-12-30 ', '18:30:00') WHERE EndTime = CONCAT('0000-00-00 ', '00:00:00')";
I also tried
$sql = "UPDATE `daycare attendance table` SET EndTime = CONCAT('1899-12-30 ', '18:30:00') WHERE EndTime = ''";
and
$sql = "UPDATE `daycare attendance table` SET EndTime = CONCAT('1899-12-30 ', '18:30:00') WHERE EndTime IS NULL";
The script just errors out as
Warning: mysql_db_query(): supplied argument is not a valid MySQL-Link resource in...
any help?
Since it runs daily, you could just say where end_time < now() - INTERVAL 24 HOUR and start_time >= now() - INTERVAL 24 HOUR;
Ok, updated a sample fiddle for you using timestamp -> http://sqlfiddle.com/#!2/bbdfe/1
Probly the best thing to do is to change your default from '0000-00-00 00:00:00' which isn't valid for a timestamp field to a valid time -> 1970+ and all your problems will then go away.
Related
I have an SQL table containing fields with date and time with columns start and end as DateTime.
What I want is a query that will select the row where the current date and time is between the value of start and end fields plus the type is WE.
Example:
If current date and time is 2022-05-18 15:30:00, row number 39 should be displayed as a result.
So far, this is the code I came up with but it returns zero result:
select * from `examdates` where (NOW() between `start` and `end`) and `type`='WE'
I also tried some answers I found in this forum like
select * from `examdates` where NOW() >= `start` and NOW() <=`end` and `type`='WE'
but result is still
Thanks!
Test this:
SELECT *
FROM `examdates`
WHERE CONVERT_TZ(NOW(), ##time_zone, '+00:00') BETWEEN `start` AND `end`
and `type`='WE'
The function retrieves server timezone info from session variable and converts the server local datetime to GMT time zone. If the values in the table are ones of some another timesone then adjust 3rd function parameter accordingly.
See https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=b054129c8210336d70b6f0a4ecc50b5d
I want to UPDATE time only from datetime using sql but it seem cant work. the update will get from user and only change the time.
example 2018-10-06 08:00:00 update to 2018-10-06 12:00:00 (time that user enter)
$sql3="UPDATE course
SET date_start = '$date_start'
WHERE date_start = SUBSTRING(date_start,11,17)
AND CourseNo = '$id1' ";
$hasil3=mysql_query($sql3);
if($hasil3==false)
echo "SQL error:".mysql_error();
else
{
?> <center>
<script language="javascript">
alert("Successfully update!");window.location="studentTimetable.php";
</script>
You can use an expression such as:
select date('2018-10-06 08:00:00') + interval 12 hour
You can also use addtime() if you want to add hours/minutes/seconds.
This should be simple enough to put into an update statement if that is what you want to do.
If I understand you right, you get an input as string in the format <hours> ":" <minutes> ":" <seconds>, that represents a time of the day. You want to replace the time of the day portion of a datetime column with that given time of the day.
To do so you can first downcast the column to a date and then upcast it again to a datetime. That way the time of the day portion becomes 00:00:00. Now use date_add to add the time of the day the user has given to you. Since the time of the day was zeroed before that will result in a datetime with the time of the day as the user put in.
UPDATE elbat
SET nmuloc = date_add(cast(cast(nmuloc AS date) AS datetime), INTERVAL '12:00:00' HOUR_SECOND);
I had a similar problem to this..
I have a number of dates that are not quite right by a few seconds. They should all end on an Hour, i.e 00:00
Work out how much time I need add (I know i want to add some seconds)
SELECT 60 - DATEPART(SECOND, EndDate), e.*
FROM Events e
WHERE DatePart(SECOND, EndDate) <> 0
Now I can write an update to correct all the dates that are slightly off.
UPDATE Events
SET EndDate = DATEADD(SECOND, i.Seconds, i.EndDate)
FROM (
SELECT Id, 60 - DATEPART(SECOND, EndDate) AS Seconds, EndDate
FROM Events e
WHERE DatePart(SECOND, EndDate) <> 0
) i
WHERE
i.ID = Events.ID
Im trying to come up with an SQL query that will list the rows that are within a certain time span which depends on the current time. The function which will call this query will check if the user is in the system, otherwise it will list him as absent.
So a class starts at 9:00 for example, the query should compare the ScheduleTimeStart and ScheduleTimeEnd, with the current time. The query should list a row only if the time is 9:00 and onwards, but do not list a row if the current time exceeded the 30 minutes cooldown period. So in other words from 9:00 - 9:30.
What I have done so far:
SELECT studentID, scheduleID, scheduleDate, scheduleTimeStart, scheduleTimeEnd, schedule.classID, schedule.unitsID
FROM schedule
JOIN schedule_students USING(scheduleID)
JOIN students USING(studentID)
WHERE scheduleDate=CURDATE()
AND scheduleTimeStart >= CURTIME() //Gordons answer
AND scheduleTimeStart < CURTIME() + interval 30 minute// but its not working
AND studentID=1003;
First, learn to use parameters. Don't munge SQL strings with values. This can lead to syntactic errors and SQL injection vulnerabilities.
SELECT s.*
FROM schedule s
WHERE scheduleID = ? AND classID = ? AND unitsID = ? AND
DATE(scheduleDate) = CURDATE() AND
scheduleTimeStart >= CURTIME() AND
scheduleTimeStart < CURTIME() + interval 30 minute;
It is not clear if you want the same condition on the scheduleTimeEnd as well.
I want to UPDATE my data in scheduled time. My problem is that I cant equal the date that I enter in my database in the current real time date. For example, I have 2015/24/9 19:50:00 in my database, now I want to equal it to the current real time date so that I can update a specific row in the database. If I don't do that, the amount field will just multiply 5 in every row. I want to multiply the amount by 5 in a specific row and time
Code:
CREATE EVENT myeventsdasa11s
ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 1 MINUTE
ON COMPLETION PRESERVE
DO
UPDATE messagesd
SET amount = amount*5
WHERE DATE = (the the current real time date);
DATETIME and TIMESTAMP values are, like floating-point values, difficult to compare for numerical equality. In other words, if it happens that NOW() = datetimestamp, it's a lucky accident. This is especially true when processing events: the event actually starts to run shortly after the scheduled time.
So, instead of saying something like this
`DATE` = NOW()
say something like this
`DATE` BETWEEN NOW() - INTERVAL 10 SECOND
AND NOW() + INTERVAL 10 SECOND
Of course, such a narrow time interval makes you critically dependent on the time accuracy of the event scheduler. You'd be better off adding a LAST_UPDATED column of DATETIME type to your table, then doing this update.
UPDATE messagesd
SET amount = amount * 5,
LAST_UPDATED = `DATE`
WHERE `DATE` >= NOW() - INTERVAL 1 MINUTE
AND (`DATE` > LAST_UPDATED OR LAST_UPDATED IS NULL)
That way, every time your event runs you'll update all the rows that are due for update, but haven't yet been updated. This is not dependent on the precise time an event runs. The - INTERVAL 1 MINUTE allows the event to be up to a minute late running and still function correctly.
If you need to schedule another update for the future for a particular row, change the value of the DATE column but don't touch the LAST_UPDATED column.
Are you just looking for CURDATE()?
CREATE EVENT myeventsdasa11s
ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 1 MINUTE
ON COMPLETION PRESERVE
DO
UPDATE messagesd
SET amount = amount * 5
WHERE DATE = CURDATE();
If you need the current real date time use mysql NOW()
CREATE EVENT myeventsdasa11s
ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 1 MINUTE
ON COMPLETION PRESERVE
DO
UPDATE messagesd
SET amount = amount*5
WHERE DATE = NOW();
For the below table, i would like to get the difference between last hour and current hour for col-D and col-E for each of the site.
As part of that I am trying to first get the latest (current) hour entries for each of the site, but the following query is only listing me the entries with endTime as 01:00:00, when i have entries upto 9.00AM
select distinct(mmeName), endDate, endTime, c_ratio, set_time from attach where
type='INIT' and Date(endDate)=curDate() and
Time(endTime) >= DATE_ADD(Time(endTime), INTERVAL -1 HOUR) group by mmeName;
Any help would be appreciated for the immediate issue and as well finding the difference between current and last hour.
EDITED
I think this is what you are looking for. This will give you any records where the endTime is one hour prior to the latest current time for each mmeName. The 'max' sub-select gets the latest end datetime for each mmeName, and the join back matches on record exactly one hour prior to that.
SELECT mmeName, endDate, endTime, c_ratio, set_time
FROM attach a
JOIN
(SELECT mmeName, CONCAT(endDate, ' ' , endTime) max_endDateTime
FROM attach
WHERE type = 'INIT'
ORDER BY endDate DESC, endTime DESC
) AS max ON max.mmeName = a.mmeName
AND max.max_endDateTime = DATE_ADD(CONCAT(endDate, ' ' , endTime), INTERVAL 1 HOUR)
WHERE type = 'INIT'
;
ORIGINAL
select mmeName, endDate, endTime, c_ratio, set_time
from attach
where type='INIT' and Date(endDate)=curDate() and
endTime >= DATE_SUB(now(), INTERVAL -1 HOUR)
group by mmeName;
Note: If there are multiple matching records for a given mmeName, this query will just grab one of them.
EDITED: You need drop the TIME() functions from the WHERE clause. Both would have the date and time and if you didn't, if you ran it between 12:00 AM to 1:00 AM it would not return any results.