简体   繁体   English

mysql插入一个select

[英]mysql insert with a select

I am writing in PHP a mySQL query. 我在PHP中编写了一个mySQL查询。 I would like to duplicate a row in a table in the database. 我想在数据库中的表中复制一行。 I would also like to insert a time stamp as to when that duplication happened with the NOW() function. 我还想插入一个时间戳,以确定何时与NOW()函数发生重复。

If I had a table named 'people' stuctured with the fields: 如果我有一个名为'people'的表被字段结构化:

id, name, phone, dateCreated

and I wanted to duplicate this record I would do the following: 我想复制这条记录我会做以下事情:

INSERT INTO people (id, name, phone, dateCreated)
SELECT id, name, phone, dateCreated 
FROM people 
WHERE id = '$id'

This will duplicate the record identified by the $id. 这将复制$ id标识的记录。 I would like to do this same concept but instead of copying the dateCreated, I would like the query to insert the current datetime via the NOW() function but I am not sure how to structure this type of query. 我想做同样的概念但不是复制dateCreated,我希望查询通过NOW()函数插入当前日期时间,但我不知道如何构造这种类型的查询。

Something like: 就像是:

INSERT INTO people (id, name, phone)
SELECT id, name, phone
FROM people
WHERE id = '$id'
(dateCreated) value (NOW())

How would I structure the query? 我将如何构建查询?

INSERT INTO people (id, name, phone, dateCreated)
SELECT id, name, phone, NOW()
FROM people 
WHERE id = '$id'

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

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