开发者

mysql insert with a select

开发者 https://www.devze.com 2023-01-25 17:09 出处:网络
I am writing in PHP a mySQL query.I would like to duplicate a row in a table i开发者_Go百科n the database.I would also like to insert a time stamp as to when that duplication happened with the NOW() f

I am writing in PHP a mySQL query. I would like to duplicate a row in a table i开发者_Go百科n the database. I would also like to insert a time stamp as to when that duplication happened with the NOW() function.

If I had a table named 'people' stuctured with the fields:

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. 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.

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'
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号