mysql insert with a select
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'
精彩评论