
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
How to update a timestamp field of a MySQL table?
Let us first create a table −
mysql> create table DemoTable -> ( -> PunchOut timestamp, -> PunchStatus tinyint(1) -> ); Query OK, 0 rows affected (0.51 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('2019-01-31 6:30:10',1); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable values('2019-02-06 4:10:13',0); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('2018-12-16 03:00:30',0); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values('2016-11-25 02:10:00',1); Query OK, 1 row affected (0.22 sec)
Display all records from the table using select statemen −
mysql> select *from DemoTable;
Output
+---------------------+-------------+ | PunchOut | PunchStatus | +---------------------+-------------+ | 2019-01-31 06:30:10 | 1 | | 2019-02-06 04:10:13 | 0 | | 2018-12-16 03:00:30 | 0 | | 2016-11-25 02:10:00 | 1 | +---------------------+-------------+ 4 rows in set (0.00 sec)
Here is the query to update the timestamp field of a MySQL table. We have set current date to the fields with PunchStatus 0 −
Note − Current date and time is 2019-06-30 13:43:45
mysql> update DemoTable set PunchOut=now() where PunchStatus=0; Query OK, 2 rows affected (0.19 sec) Rows matched: 2 Changed: 2 Warnings: 0
Let us check the table records once again −
mysql> select *from DemoTable;
Output
+---------------------+-------------+ | PunchOut | PunchStatus | +---------------------+-------------+ | 2019-01-31 06:30:10 | 1 | | 2019-06-30 13:43:45 | 0 | | 2019-06-30 13:43:45 | 0 | | 2016-11-25 02:10:00 | 1 | +---------------------+-------------+ 4 rows in set (0.00 sec)
- Related Articles
- Creating a table with a TIMESTAMP field in MySQL?
- How to update of incorrect timestamp format in a DB2 table?
- How to search for a date in MySQL timestamp field?
- How to Insert custom date into MySQL timestamp field?
- How to update date of datetime field with MySQL?
- Update a MySQL table with Java MySQL
- How to batch update MySQL table?
- Selecting from a MySQL table based on parts of a timestamp?
- How can I update a field in a MySQL database table by adding a value in the second table with a value from the first table?
- How can we update values in a MySQL table?
- How to update MySQL table storage engine
- How to update a MySQL table by swapping two column values?
- MySQL query to update only a single field in place of NULL
- How do I update NULL values in a field in MySQL?
- How to add a “created at” column in a table to set the timestamp in MySQL?

Advertisements