What i basically need is a query that will allow me to display the time that has passed between last row inserted and the current time.
The specific table uses the timestamp field type.
I know that DATE_SUB must be used here, but i have no idea how to in this particular problem
Assuming that table has a timestamp field which records the time-of-insertion for each record, then
SELECT TIMEDIFF(now(), max(timestampfield))
FROM yourtable
select min(now() - timestamp) from table
Related
For example if a user inserts '2017-03-13 12:16:18.0' into the timestamp column,
the same user should not be allowed to enter another value in this column IF IT'S ON THE SAME DAY i.e 2017-03-13 (in this case). Or ultimately, update the timestamp column with the previously inserted value ('2017-03-13 12:16:18.0') each time the user tries to insert a timestamp date twice ON THE SAME DAY. I hope I've been explicit enough.
Below is a non-functioning query I came up with, but it shows what I would like the query to do ultimately. Thanks for your help and feedbacks.
INSERT INTO hr.entry(id,entry_time)
VALUES (45,
CASE WHEN '13-03-2017'= CAST(SYSDATE() AS date) THEN
(UPDATE hr.entry
SET entry_time =
(SELECT entry_time
FROM hr.entry
WHERE id=45
AND CAST(entry_time AS date)= CAST(SYSDATE() AS date) )
ELSE
SYSDATE());
You could add a DATE column to your table, and add a unique index to that column. Then, when you insert the timestamp into the timestamp column, you could also insert the date from that timestamp into the DATE column. Attempts to insert a timestamp whose date component already exists in that table would cause MySQL to throw an error.
I think you are going to need a trigger, unless you store the timestamp as a string using YYYY-MM-DD HH:MI:SS format. I don't really recommend that.
So, create a trigger that updates a column called timestamp_date. This simply extracts the date part of the timestamp.
With this column, you can define a unique index:
create unique index entry_userid_timestampdate on entry(userid, timestamp_date);
This will then enforce your condition.
If you decide that you want to store the timestamp as a string, you don't need the trigger (although will need to manually set the "timestamp"). Instead, you can use a prefix:
create unique index entry_userid_timestampstr on entry(userid, left(timestamp_date, 10));
I have a table mytable that is continuously being updated with new records.
I'm trying to get the most recent records using the method below ([lastId] is largest id of the previous select):
SELECT *
FROM mytable
WHERE id > [lastId]
ORDER BY id DESC
But I believe ids (and timestamps if I use it) are not necessarily inserted in order so there is a (small) possibility of missing some records. How can I get around this?
--EDIT--
The id field is AUTOINCREMENT.
Can you add a timestamp to the table and let the process that inserts records insert with current time?
I want to automatically delete recovery_url from table users 30 minutes after it's been updated. Is there any way of doing this?
Thanks
One way I can think of is using an additional column indicating how long the recovery_url is valid. It can be a datetime column and in your queries you can select the recovery_url depending on that date. So you don't need any triggers or events.
select case when url_valid_until < now()
then null
else recovery_url
end as recovery_url
from your_table
I'm wondering if it is possible to find all new rows in a table that were added in the last week if the table has no date column to signify when a given row was inserted into the table?
If so, can someone please advise me on how to accomplish this?
Or do I require a date column?
Thanks
You do need some kind of timestamp, Or keep external records somewhere, what was added last week ;-). Do you have some sort of auto-incrementing column id?
You do need a date col. 1 week means u are specifying time duration which requires dates or time field.
Otherwise you can keep a Status Column whose values are active or inactive. Each time row is displayed to user, you can update the status to inactive
You definitely need a date column. You could add one today, allowing all previous entries in the table to be null for that field, and have it start populating from now. After a week, your problem is solved.
Your best option is a timestamp, then you can select any particular period.
Alternatives would be an autoincrement field and you record the last number of the previous wee.
Or you have a status field that defaults to "NEW" and then you set it to "OLD" or whatever once you have processed it.
auto-incrementing id can be helpful.save a record id(last inserted record id).After a week compare id's with that specific record id will result you the newer rows as all the rows with larger id's will be of last week
Is there any way using which I can get the last row I have updated of the table.
For Ex:- my table has 1000 records in it.
I have updated the value of 500th record of table X.
So, I want the ID 500 in return.
Thanks in advance.
Regards,
I don't think this feature exists with MySQL. However you can get the same effect by adding a timestamp column:
ALTER TABLE yourtable
ADD COLUMN last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
This would make the last_update column pretty much automatically maintained. Now you can select from yourtable the last updated row based on the timestamp.