I don't know if the title is clear but I'll try to explain it here. We have thousands of record for our database and there are a couple of datetime columns in theses tables. We have to move to int using unix_timestamp but I can't figure out a query in MySQL that would update these fields.
Let's say I have a simple table with a field like that :
user_table :
id : int,
name : string,
date_joined : datetime;
To change all of these date_joined filed to a int I thought adding a int column and copying the date as a int into the new column then drop the old column and rename my new column could be a solution. I built a little php script my script will then make thousands of MySQL query which is quite long. My script would run queries like :
UPDATE user_table SET date_joined_int=UNIX_TIMESTAMP(date_joined) where id = 1
...
Is this possible to do this with one MySQL query?
You right, you should add new INT
column
ALTER TABLE user_table ADD date_joined_int INT(11) not null;
Than convert your dates
UPDATE user_table SET date_joined_int = UNIX_TIMESTAMP(date_joined);
And finally remove date_joined
column, and remane date_joined_int
to date_joined
You're so close. Just drop the WHERE clause to update all the rows in a single query.
UPDATE user_table
SET date_joined_int = UNIX_TIMESTAMP(date_joined)
If you remove the WHERE
clause, the UPDATE
will be applied to all rows in the table.
UPDATE user_table SET date_joined_int=UNIX_TIMESTAMP(date_joined)