Jason
Jason

Reputation: 2727

mysql insert with a select

I am writing in PHP a mySQL query. 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.

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?

Upvotes: 3

Views: 191

Answers (1)

cdhowie
cdhowie

Reputation: 169518

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

Upvotes: 3

Related Questions