简体   繁体   English

如何插入减去秒的日期时间列?

[英]How do I insert into a datetime column minus the seconds?

So I'm working on ac# project that creates row in a table with a datetime column and then extract that auto generated ID of that column afterwards.所以我正在研究 ac# 项目,该项目在带有日期时间列的表中创建行,然后提取该列的自动生成的 ID。 My way of extracting the ID is through the code below the problem is the length it takes to execute the first query and then the second query is more than 1 second so I end up getting empty rows.我提取 ID 的方法是通过下面的代码,问题是执行第一个查询所需的长度,然后第二个查询超过 1 秒,所以我最终得到空行。 So how do I insert or select rows minus the seconds?那么如何插入或选择行减去秒?

INSERT INTO transactionlog(transactionDate) VALUES(NOW())

AND THEN IMMEDIATELY THIS然后立即这个

SELECT transactionID,transactionDate FROM transactionlog WHERE transactionDate=NOW();

NOW() gives you the date/time when the statement it contains started . NOW()为您提供它包含的语句开始时的日期/时间。 While it is guaranteed that several invokations in the same query return the same value, this is not true accross queries.虽然可以保证同一查询中的多个调用返回相同的值,但这查询中并非如此。

To get the auto-incremented value generated during an insert, you can use LAST_INSERT_ID() instead:要获取插入期间生成的自动递增值,您可以使用LAST_INSERT_ID()代替:

INSERT INTO transactionlog(transactionDate) VALUES(NOW());
SELECT LAST_INSERT_ID();

You can utilize mysql DATE_FORMAT() function to control to format.您可以使用 mysql DATE_FORMAT() 函数来控制格式。

SELECT transactionID,transactionDate FROM transactionlog WHERE transactionDate=DATE_FORMAT(NOW(), '%c/%e/%Y %H:%i');

You can have a look here at: https://www.w3resource.com/mysql/date-and-time-functions/mysql-date_format-function.php您可以在此处查看: https : //www.w3resource.com/mysql/date-and-time-functions/mysql-date_format-function.php

if you want to play with the format specifier如果你想使用格式说明符

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM