
- 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
Fetch student records whose result declared 12 days before the current date in MYSQL
For this, you need to compare and find the difference between the current date and the result date of students. This can be done with AND operator along with DATEDIFF().
Let us first create a table −
mysql> create table DemoTable1547 -> ( -> StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> StudentName varchar(20), -> StudentMarks int, -> StudentResultDeclareDate datetime -> ); Query OK, 0 rows affected (0.55 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1547(StudentName,StudentMarks,StudentResultDeclareDate) values('Chris',56,'2019-10-13 13:00:00') -> ; Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable1547(StudentName,StudentMarks,StudentResultDeclareDate) values('Bob',60,'2019-10-13 12:00:00'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable1547(StudentName,StudentMarks,StudentResultDeclareDate) values('Mike',45,'2019-10-13 14:00:00'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable1547(StudentName,StudentMarks,StudentResultDeclareDate) values('Carol',78,'2019-10-01 14:00:00'); Query OK, 1 row affected (0.11 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable1547;
This will produce the following output −
+-----------+-------------+--------------+--------------------------+ | StudentId | StudentName | StudentMarks | StudentResultDeclareDate | +-----------+-------------+--------------+--------------------------+ | 1 | Chris | 56 | 2019-10-13 13:00:00 | | 2 | Bob | 60 | 2019-10-13 12:00:00 | | 3 | Mike | 45 | 2019-10-13 14:00:00 | | 4 | Carol | 78 | 2019-10-01 14:00:00 | +-----------+-------------+--------------+--------------------------+ 4 rows in set (0.00 sec)
The current date is as follows −
mysql> select curdate(); +------------+ | curdate() | +------------+ | 2019-10-13 | +------------+ 1 row in set (0.00 sec)
Following is the query to fetch student records whose result declared 12 days before the current date −
mysql> select * from DemoTable1547 where datediff(curdate(),StudentResultDeclareDate) >=12 and StudentMarks > 50;
This will produce the following output −
+-----------+-------------+--------------+--------------------------+ | StudentId | StudentName | StudentMarks | StudentResultDeclareDate | +-----------+-------------+--------------+--------------------------+ | 4 | Carol | 7 8 | 2019-10-01 14:00:00 | +-----------+-------------+--------------+--------------------------+ 1 row in set (0.00 sec)
- Related Articles
- MySQL query to fetch date records greater than the current date after adding days with INTERVAL?
- Fetch records from interval of past 3 days from current date in MySQL and add the corresponding records
- MySQL query to fetch records wherein timestamp is before 15+ days?
- Calling NOW() function to fetch current date records in MySQL?
- Fetch date records comparing with the current date’s day and month in MySQL
- Selecting records 15 days before today in MySQL?
- Select a column if condition is met in MySQL to fetch records from current date and current date + 1
- Display Timestamp before the current date in MySQL
- How to get the records of the last two days from the current date in MySQL?
- MySQL time period query to fetch date records from interval of 14 weeks from current date?
- MySQL query to fetch records before currentdate + 2 weeks?
- Comparison of varchar date records from the current date in MySQL
- Add 11 days to current date in MySQL
- Fetch datetime row from exactly past 7 days records in MySQL
- Query MySQL table and fetch rows posted before the last 3 days?

Advertisements