复制行时默认插入日期列的 INTO 值,如何操作?

发布于 2024-12-14 08:56:23 字数 323 浏览 0 评论 0原文

晚上好,

我用 php、mysql 和 ajax 制作了一个运行良好的函数。链接执行查询以复制条目。该查询当前已成功执行,

$sql = "INSERT INTO players(day, name, lastname ) SELECT day, name, lastname FROM players WHERE id=$id"; mysql_query( $sql);

“day”列是日期列。我想插入当前日期($now),而不是属于该行的日期。有点默认值,到目前为止找不到类似的东西。可能很简单,有什么建议吗?:)

提前致谢!

Good evening,

I made a well working function with php, mysql and ajax. A link executes a query to copy a entry. This query is currently successfully executed

$sql = "INSERT INTO players(day, name, lastname ) SELECT day, name, lastname FROM players WHERE id=$id"; mysql_query( $sql);

the column 'day' is the date column. Instead of the date that belongs to the row, I want to insert the current date ($now). Sort of default value, couldn't find anything like this so far. Probably simple, any advice ?:)

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

擦肩而过的背影 2024-12-21 08:56:23

您可以在 SELECT 列表中包含非列的内容。当前日期应以 current_date 形式提供,因此此 SQL 应该可以解决问题:

insert into players (day, name, lastname)
select current_date, name, lastname
from players
where id = $id

如果您碰巧在 PHP 变量 $this_date 中有所需的日期,并且它位于正确的格式,那么你可以使用这个:

insert into players (day, name, lastname)
select '$this_date', name, lastname 
from players
where id = $id

You can include things in a SELECT list that aren't columns. The current date should be available as current_date so this SQL should do the trick:

insert into players (day, name, lastname)
select current_date, name, lastname
from players
where id = $id

If you happen to have the date you wanted in the PHP variable $this_date and it was in the right format, then you could use this:

insert into players (day, name, lastname)
select '$this_date', name, lastname 
from players
where id = $id
只等公子 2024-12-21 08:56:23

您可以使用内置的 MySQL Date 和 Now 或 CURDATE 函数。如果该列是日期时间,则只需使用 NOW()。

$sql = "INSERT INTO players(day, name, lastname ) SELECT NOW(), name, lastname FROM players WHERE id=$id"; mysql_query( $sql);

MySQL NOW文档 列是日期,然后将 DATE() 函数与 NOW() 一起使用,甚至更容易使用 CURDATE() 函数。
MySQL CURDate 文档

    $sql = "INSERT INTO players(day, name, lastname ) SELECT CURDATE(), name, lastname FROM players WHERE id=$id"; mysql_query( $sql);

    $sql = "INSERT INTO players(day, name, lastname ) SELECT DATE(NOW()), name, lastname FROM players WHERE id=$id"; mysql_query( $sql);

MySQL 日期文档

You can use the builtin MySQL Date and Now or CURDATE functions. If the column is a DateTime then just use NOW(). MySQL NOW Documentation

$sql = "INSERT INTO players(day, name, lastname ) SELECT NOW(), name, lastname FROM players WHERE id=$id"; mysql_query( $sql);

If the column is a Date then use the DATE() function with NOW() inside or even easier the CURDATE() function.
MySQL CURDate Documentation

    $sql = "INSERT INTO players(day, name, lastname ) SELECT CURDATE(), name, lastname FROM players WHERE id=$id"; mysql_query( $sql);

    $sql = "INSERT INTO players(day, name, lastname ) SELECT DATE(NOW()), name, lastname FROM players WHERE id=$id"; mysql_query( $sql);

MySQL Date Documentation

月下凄凉 2024-12-21 08:56:23

MSSQL 有 getdate() 来实现同样的目的。

MSSQL has getdate() for the same.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文