
- 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
Select data and set value to boolean based on timestamp column in MySQL
For this, use IF(). Let us first see the current date −
mysql> select curdate(); +------------+ | curdate() | +------------+ | 2019-12-10 | +------------+ 1 row in set (0.00 sec)
Let us first create a table −
mysql> create table DemoTable1890 ( DueDate timestamp ); Query OK, 0 rows affected (0.00 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1890 values('2017-12-10'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1890 values('2021-12-10'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1890 values('2018-04-24'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1890 values('2020-05-11'); Query OK, 1 row affected (0.00 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable1890;
This will produce the following output −
+---------------------+ | DueDate | +---------------------+ | 2017-12-10 00:00:00 | | 2021-12-10 00:00:00 | | 2018-04-24 00:00:00 | | 2020-05-11 00:00:00 | +---------------------+ 4 rows in set (0.00 sec)
Here is the query to select data and set value based on timestamp column −
mysql> select if(DueDate > curdate() - interval 2 year,false,true) as Status from DemoTable1890;
This will produce the following output −
+--------+ | Status | +--------+ | 1 | | 0 | | 0 | | 0 | +--------+ 4 rows in set (0.00 sec)
- Related Articles
- How to extract a data frame’s column value based on a column value of another data frame in R?
- Perform multiplication in SELECT depending on column value in MySQL?
- How to select the first and last row based on group column in an R data frame?
- Exclude rows based on column value when another duplicate column value is found in MySQL?
- How to assign a column value in a data frame based on another column in another R data frame?
- MySQL DATE_ADD() to increment a date based on the value in another column?
- How to find the position of a data frame’s column value based on a column value of another data frame in R?
- Show column value twice in MySQL Select?
- How to select rows based on range of values of a column in an R data frame?
- How to select top rows of an R data frame based on groups of factor column?
- Do a select in MySQL based only on month and year?
- MySQL SELECT to sum a column value with previous value
- How to select date from timestamp in MySQL?
- How to create boolean column in MySQL with false as default value?
- Set current date and time to timestamp in MySQL

Advertisements