【问题标题】:Insertion time of a row in MYSQLMYSQL中一行的插入时间
【发布时间】:2015-04-07 05:02:26
【问题描述】:

有没有什么方法可以获取表格中特定行的insertion time信息?

假设,我有以下 表(用户)

      ________________
      | ID  | name   |
      |_____|________|
      | 1   |    tom |
      | 2   |   brad |
      |_____|________|

现在,我想知道插入第二行(2,brad)的时间

有可能吗?任何帮助表示赞赏!

【问题讨论】:

标签: mysql


【解决方案1】:

你可以试试这个:

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

这将给出表中最后插入时间的时间。

附带说明:

您可以在表中再添加一列,并将插入记录时的时间存储在该列中。这将帮助您确定插入特定记录的时间。

类似这样的:-

insert into yourtable(ID, name, CreatedTime)
values(2,'Brad',now())

【讨论】:

    【解决方案2】:

    您必须使用trigger

    https://dev.mysql.com/doc/refman/5.5/en/trigger-syntax.html

    这有点复杂,但确实有效!

    delimiter |
    
    CREATE TRIGGER trigger_user_insert BEFORE INSERT ON user
      FOR EACH ROW
      BEGIN
        -- When an insertion occurs, you do your work here with SQL
      END;
    |
    
    delimiter ;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-13
      • 2013-07-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多