I have a mysql users table with column join_date in a format "d/m/Y". I just want to update this column to a timestamp of that existed data.
id name join_date
1 john 08/02/2014
Now I need a code to run (like while loop or foreach) to update all dates data in column join_date to a timestamp [time():] as below:
id name join_date
1 john 1391814000
Any suggestion guys? Thanks in advance.
In the comments we could find out that not TIMESTAMP but DATE is the best datatype for your application. Assuming that join_date is a varchar column, you can issue the following SQL commands in order to change the column type and convert the data.
First you need to add a new, temporary, column:
ALTER TABLE `users` ADD `temp_date` DATE;
Then convert the existing strings to DATE values:
UPDATE `users` SET `temp_date` = STR_TO_DATE(`join_date`, '%c/%e/%Y');
Now you can drop the existing column:
ALTER TABLE `users` DROP `join_date`
And finally rename the temporary column:
ALTER TABLE `users` CHANGE `temp_date` `join_date` DATE;
Related
I have an admin users db, I want to create a column with type datetime for edits, to know when and admin acc was edited, I will introduce the time with php and MySQLi, im on phpmyadmin, when I try to create the column it says:
#1292 - Incorrect datetime value: '0000-00-00 00:00:00' for column 'editado' at row 1
MySQL code is:
ALTER TABLE `admins` ADD `editado` DATETIME NOT NULL AFTER `password`;
Tried to execute on SQL but nothing, how can I do this?
If the table is not empty when you add a column, you need to first add the column as NULLABLE, update all the records in the table assigning a value to the column, then you can change the column you added from NULLABLE to NOT NULL. So something like this should be done:
ALTER TABLE admins ADD editado DATETIME NULL AFTER password;
UPDATE admins SET editado = '1900-01-01 00:00:00';
ALTER TABLE admins MODIFY editado DATETIME NOT NULL;
Not sure if the last query is correct syntax as I don't write MySQL often but thats what should happen.
I am trying to insert into a column if the column value is not null.
I have my table:
create table clock(
usr_id int,
clock_id int AUTO_INCREMENT,
clock_in date,
clock_in_time time,
clock_out date,
clock_out_time time,
FOREIGN KEY (usr_id)references df_user(usr_id),
primary key(clock_id)
);
I am trying to grab the last entry of the table and checking whether or not it is null.
SET #lastEntry = (SELECT clock_id FROM clock WHERE clock_id=(SELECT max(clock_id) FROM clock));
SET #clock_value = (select clock_out from clock where clock.clock_id = #lastEntry);
If the last entries clock out column is NULL (#clock_value IS NULL) then insert into the table.
if #clock_value IS NULL THEN insert into clock (clock_out, clock_out_time) values (curdate(), curtime());
Can someone please tell me the correct way of doing this process? SQL is telling me that the query is wrong. Thanks all!
What you need seems to be an update clause.
update clock set clock_out=curdate(), clock_out_time=curtime() ;
or something like that. You cannot insert here, because when inserting you create new rows.
I unable to update current time(NOW) in last_updated cloumn.
Because i have read this query from text file.
INSERT OR REPLACE INTO table_name (kb_corporate_guid,kb_user_guid,
name,last_updated)
VALUES ('100','121','FAQ','2013-02-07 07:06:05');
I want to ignore last_updated cloumn value '2013-02-07 07:06:05' even if i specified in query and replace with NOW() value into last_updated cloumn.
I tried this one but it won't work for me.
ALTER TABLE table_name
CHANGE `last_updated` `last_updated` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE CURRENT_TIMESTAMP;
Option 1
You may create trigger on insert/update which will update last_updated field with MySql NOW() function, it will overwrite the field value, but it might slow down the process when you do bulk insert/update.
Option 2
Find and Replace the text for last_updated field and timestamp values from text file.
Option 3
Create temporary table with same schema and import into temporary table then use INSERT INTO main_table SELECT corp_id, user_id, name, NOW() FROM temp_table table to insert into main table.
Default is used when the value is not sent in the query, use now in the query instead of timestamp..
First I have to create a column in the end of table
If there exist the data in the original column , move them into the new column
If the original column has no data now
(because they must be nullable , so that there is chance the column has no data, the column will be deleted)
The problems are:
move from col to new col is insert into new col {select old colmn from table} ?
Then how can I check the column exist no data / all col is null.
Also, How can I check datatype and whether it is null in pdo ?
Thank you
If you want to add a column to a table:
alter table myTable add column 'columnName' <column-specifications>
If you want to insert values from another column into this column:
update myTable set columnName=originalColumn
If you want to remove a column from a table:
alter table myTable drop column columnName
That being said, like Jeff Paquette mentioned, are you sure you want to create a new column? I don't really understand the point of creating a column to insert values from another column. Won't renaming your original column do the exact same thing?
alter table myTable change column ...
How can I convert a column with TIMESTAMP to DATETIME and retain the dates? I'm using PHPMyAdmin.
Also timestamp had the option to autofill, is there a way to do so in DATETIME? Or should I insert it each time with PHP?
thanks
If this query
ALTER TABLE table CHANGE `time` `time` datetime
will lose dates you can create new column then assign old values then delete old column and rename new one
ALTER TABLE table ADD `datetime` datetime AFTER `time`;
UPDATE table set datetime=time;
ALTER TABLE table DROP datetime;
ALTER TABLE CHANGE `datetime` `time` datetime
Read this:
This means, for example, that you cannot set the default for a date column to be the value of a function such as NOW() or CURRENT_DATE.
ALTER TABLE tablename MODIFY COLUMN columnname DATETIME;
(test on a test table first...)
No way to set the default to the current time save for triggers:
DELIMITER $
CREATE TRIGGER tablename_before_insert BEFORE INSERT ON tablename
FOR EACH ROW
BEGIN
IF NEW.columnname IS NULL THEN
SET NEW.columnname = NOW();
END IF;
IF NEW.datum = '0000-00-00 00:00:00' THEN
SET NEW.columnname = NOW();
END IF;
END$
DELIMITER ;
Use FROM_UNIXTIME() to convert from a unix timestamp into a regular datetime value.
A unix timestamp has no native type in mysql - it's simply an integer. timestamp fields will auto-populate, but only the first one in any given table. More details on that here.