
- 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
MySQL Select where timestamp is in the current hour?
For this, you can use CURTIME(). Let us first create a table −
mysql> create table DemoTable -> ( -> ArrivalTime timestamp -> ); Query OK, 0 rows affected (0.65 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('2019-10-26 17:55:55'); Query OK, 1 row affected (0.46 sec) mysql> insert into DemoTable values('2019-10-26 18:00:00'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values('2019-10-26 18:55:00'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('2018-10-26 16:00:10'); Query OK, 1 row affected (0.20 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+---------------------+ | ArrivalTime | +---------------------+ | 2019-10-26 17:55:55 | | 2019-10-26 18:00:00 | | 2019-10-26 18:55:00 | | 2018-10-26 16:00:10 | +---------------------+ 4 rows in set (0.00 sec)
The current time is as follows −
mysql> select curtime(); +-----------+ | curtime() | +-----------+ | 18:43:55 | +-----------+ 1 row in set (0.00 sec)
Following is the query to select where the timestamp is in the current hour −
mysql> select *from DemoTable where hour(ArrivalTime)=hour(curtime());
This will produce the following output −
+---------------------+ | ArrivalTime | +---------------------+ | 2019-10-26 18:00:00 | | 2019-10-26 18:55:00 | +---------------------+ 2 rows in set (0.00 sec)
- Related Articles
- How to select all records that are 10 minutes within current timestamp in MySQL?
- Display Timestamp before the current date in MySQL
- Select timestamp as date string in MySQL?
- How to select date from timestamp in MySQL?
- Get timestamp date range with MySQL Select?
- MySQL query to select date from timestamp?
- SELECT WHERE IN null in MySQL?
- Set current date and time to timestamp in MySQL
- Select entries with timestamp after X time in MySQL
- Delete records where timestamp older than 5 minutes in MySQL?
- Get current year in MySQL WHERE clause?
- MySQL Select where values IN List string?
- MySQL select query with multiple WHERE?
- How to select part of a Timestamp in a MySQL Query?
- Select last day of current year in MySQL?

Advertisements