My problem is in brief here...
Once a user signed in i stored his login date in the users table. If the user doesn't logged in for 72 hours i need to change his status to inactive.
How can i able to find whether 72 hours is completed or not after the user logged in using My Sql.
Thanks in advance...
I'd recommend using the TIMEDIFF() function, which you can find documented here:
dev.mysql.com timediff doc.
In your case, I'd format my where clause something like this:
WHERE
TIMEDIFF(CURTIME(), LastLoginDate) > '3 0:0:0.0'
or
WHERE
TIMEDIFF(CURTIME(), LastLoginDate) > '72:0:0.0'
I haven't done this specifically, but the base concept should work for you.
Create a CRON routine to run every hour with this query:
UPDATE users
SET status = 'Inactive'
WHERE (SELECT * FROM users WHERE last_login < now() - 259200)
To answer your question more specifically, it is the where clause, when ran, that tells you all the users that haven't logged in for 72 hours.
SELECT * FROM users WHERE last_login < now() - 259200
However, there is no way to set each user to inactive at exactly 72 hours. To get more accurate than the solution provided above, run the query more often.
*Note - insert your columns names where appropriate. Query not tested. 259200 = # of seconds in 72 hours - assumes you store your timestamps seconds (Epoch)
Use DATETIME type to store dates, subtract 72 hours from NOW() using DATE_SUB() and see if the result is larger than the value stored in the database.
Related
I am just learning MySql (SQL in general) and I have a question. I ran a process to populate a table with 72 records. This was done, however, I needed to run the process again and this time it populated the table again with a second record for each user for a total now of 144 records. How can I isolate the newest records created today?
A simple solution is to use current_date to figure out today's date and date() to remove the time portion of your column. Then:
where current_date = date(createdTS)
This is fine for a small dataset as yours. As general solution, you'd need a query that won't need to manipulate every row, e.g.
where createdTS >= current_date and createdTS < current_date + interval 1 day
You just have to use your createdTS column, (assuming you know what was the timestamp of both runs).
SELECT * FROM `my_table` WHERE `createdTS` > '2019-07-25 15:00:00'
You could also RANK() over and get only the newest run for each user (something like this)
Please consider the following query:
SELECT submitted_time FROM jobs WHERE timediff(NOW(), submitted_time) < '24:00:00'
My hope is for this to return all rows that have a "submitted_time" column containing a timestamp that was within the last 24 hours, However I am receiving the following results:
2017-01-18 14:58:34
2017-01-16 14:58:34
If I run the query SELECT NOW() I get 2017-01-25 18:58:32
Which appears to be correct.
What is stranger still is that I have more recent rows in the DB such as:
2017-01-24 15:17:13
Which are not being returned.
I hope I have made a glaringly obvious error that someone can point out, rather than beginning the descent into madness.
Just to be clear, the simplest and probably most performant way to handle this is (as per the link I provided in the comment)
SELECT submitted_time FROM jobs WHERE submitted_time > DATE_ADD(NOW(), INTERVAL -1 DAY);
This should be all jobs submitted literally within the last 24 hours at the moment the query is issued.
This might not be important to you for this query, but whenever you apply functions to columns in your table, any indexes you might have can not be used, because the database must run the function(s) on each value in the table before it can perform a comparison.
Using this method you figure out what the comparable datetime needs to be and mysql will use an index on submitted_time for the comparison, assuming that column is indexed appropriately.
I am looking for a way to be able to grab rows from a database from a time.
Two columns exist:
Time - scheduled Time
Delay - +seconds that time is delayed.
Let's say:
ID,time,delay
1,18:23,360
2,18:25,0
3,17:15,-60
Now, let's say I am searching for buses after actually arriving at/after 18:25, how would i do it to include these two results:
2,18:25,0
1,18:23,360 (note: 18:23 + 360 seconds = 18:29)
in a query like
where `time` >= '18:23'
Thanks
SELECT * FROM [TABLE] WHERE DATE_ADD(Time,INTERVAL TimeDelay SECOND) >= '18:25:00'
MySql Date_Add Function
In mysql, I have a user table with columns
ban_start_date [datetime]
ban_days [int]
Where this means that the user is banned from ban_start_date for another ban_days days more. How can I select the user table, but add a new column saying how many days left they are banned?
I know I need to check if NOW() is in between ban_start_date and ban_start_date+ban_days, and if so, get the difference. Something like that.
Thanks
Please try:
SELECT
DATEDIFF(DATE_ADD(banned_users.ban_start_date , INTERVAL banned_users.ban_days DAY), DATE(CURDATE())) AS ban_days_left
FROM
users as banned_users
WHERE
DATE_ADD(banned_users.ban_start_date , INTERVAL banned_users.ban_days DAY) > DATE(CURDATE());
Query takes only users who are banned at execution time and then adds the ban_days to the ban_start_date temporarily producing ban-enddate (DATE_ADD(banned_users.ban_start_date , INTERVAL banned_users.ban_days DAY)). After that DATEDIFF is used to count the days between ban-enndate and today's date, which in fact should result in ban_days_left.
Why not store the ban_end_date instead? If you want you can store the ban_days, but you should not need that value very often.
The nice thing (I think) about storing the bad_end_date is that people who are not banned will have that as a NULL. It is extremely easy to look up this one value and tell the user:
"You are banned until ban_end."
At the start of the day you will have check for people with non-NULL bad_end_date values where ban_end_date < now() and set those to NULL. Easy.
I have Rails application which using MYSQL as database. For some condition, I have to delete all the records from table which was stored exactly 2 hours before the current time.
My query is :
DELETE FROM TABLE_NAME WHERE (NOW() - created_at) > 7200;
Here create_at is datetime column type. Storing the value in the format "2012-12-04 06:39:44"
My problem is, the above query fetch the records even though the record created time is just 40 to 50 minutes and got deleted. The only problem is the record got delete after it reach 40 to 50 minx from it create time.
Can any one please correct my query. I want the MySQL solution. Please help me
You probably need this if you want to delete records created exactly 2 hours ago:
DELETE FROM TABLE_NAME WHERE created_at = NOW() - INTERVAL 2 HOUR
or this, that will delete all records created more than 2 hours ago:
DELETE FROM TABLE_NAME WHERE created_at < NOW() - INTERVAL 2 HOUR
Try this ::
DELETE FROM TABLE_NAME WHERE TIMEDIFF(NOW(),created_at) < '02:00:00';
Try:
DELETE FROM TABLE_NAME WHERE created_at<DATE_SUB(NOW(),INTERVAL 2 HOUR)
This query will delete everything created MORE THAN 2 hours ago. Putting an equal sign would mean EXACTLY 2 hours ago (in second). Of course you can format date to consider only minutes, but that would slow down the query.
If created_at is indexed (and I think it should be) don't perform any functions on it so it can use index to perform delete faster.
I understand you want to delete all records created within a time lapse. So, you shouldn't apply a "greater than" operator to the subtract operation. Instead you should try to specify an appropriated time frame.
You could also take a look to the timediff function http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_timediff
Sorry I'm not able to post the right statement for you, since I don't have a mysql server at hand.