MySQL: How can I get the timestamp of the last insertion to the database

怎甘沉沦 提交于 2019-12-31 01:50:29

问题


How can I check when was the last time (timestamp) that I wrote in the database, irrespectively of the database table that I inserted into?


回答1:


Turned out to not be the answer (can't delete as accepted). See comments below this answer.

I wasn't able to use information_schema.tables update_time as the column wasn't being updated but for create_time this worked. It would work for update_time (if changed) if update_time gets updated which might be true in your setup.

select table_schema,table_name,max_time from information_schema.tables t1 JOIN 
 (select MAX(t2.create_time) AS max_time FROM 
  information_schema.tables t2 where  
  table_schema ='test') as t3  
on t1.create_time = t3.max_time;



回答2:


Just use this SQL: SHOW TABLE STATUS; and check Update_time column.




回答3:


Add a field for modified date in the table and use LAST_INSERT_ID() to determine the last row (if you're doing it right away). Otherwise just retrieve the most recent date from the modified field for the table.




回答4:


This is basically what logging is all about.

On the CLI

  1. execute SET GLOBAL log_output = 'TABLE';
  2. execute SET GLOBAL general_log = 'ON';

Now, a table general_log inside mysql database will log all such actions on database. Use phpMyadmin or similar to view these. You can query from their results very effectively.



来源:https://stackoverflow.com/questions/9657206/mysql-how-can-i-get-the-timestamp-of-the-last-insertion-to-the-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!