MariaDB If statement in Select

In this MariaDB tutorial, we will discuss the MariaDB If Statement in Select statement and look at several examples. There are lists of the topic that comes under discussion:

  • MariaDB If Statement In Select
  • MariaDB If Statement In Select Count
  • MariaDB If Statement In Select Distinct
  • MariaDB If Statement In Select Group By
  • MariaDB If Statement In Select Greater Than
  • MariaDB If Statement In Select Json
  • MariaDB If Statement In Select Like
  • MariaDB If Statement In Select Limit
  • MariaDB If Statement In Select Multiple Columns
  • MariaDB If Statement In Select Not Working
  • MariaDB If Statement In Select Order By
  • MariaDB If Statement In Select Subquery
  • MariaDB If Then Else In Select Update
  • MariaDB If Statement In Select Where Condition
  • MariaDB If Statement In Select Yesterday
  • MariaDB If Statement In Select Year

MariaDB If Statement In Select

In this section, we will learn and understand how to use the IF function in the SELECT statement of the table by the query. And which will be explained with the help of an illustrated example.

The MariaDB IF statement is used to return one value if it is TRUE else it returns FALSE. It can be a return string or numeric value based on the expression used. The syntax of the IF statement in the MariaDB is given below:

SYNTAX:

SELECT IF (EXPRESSION, RETURN_VALUE_IF_TRUE, RETURN_VALUE_IF_FALSE) 
FROM TABLE_NAME;

The syntax explanation:

  • EXPRESSION: The expression that we want to test.
  • RETURN_VALUE_IF_TRUE: It returns VALUE if the expression turns out to be TRUE.
  • RETURN_VALUE_IF_FALSE: It returns VALUE if the expression turns out to be FALSE.

Let’s see an illustrated example of the MariaDB IF statement on the table by the following query:

EXAMPLE:

SELECT IF(PATIENT_ADMITDATE > PATIENT_DISCHARGEDATE, TRUE, FALSE) AS USING_IF_CONDITION,
PATIENT_FIRSTNAME, PATIENT_LASTNAME  
FROM johns_hopkins_hospital
LIMIT 10;

As we see in the above query, the SELECT statement is used to retrieve all records of the PATIENT_FIRSTNAME and PATIENT_LASTNAME columns from the JOHNS_HOPKINS_HOSPITAL table. The IF condition is used to test and return one value which is used within the PATIENT_ADMITDATE and PATIENT_DISCHARGEDATE columns and will return 1 or 0 values from the JOHNS_HOPKINS_HOSPITAL table for the output.

As we know that the TRUE and FALSE values are part of the BOOLEAN operator. So in the query, the TRUE value will return as 1 and the FALSE value will return as 0 for the output value.

To shorter the function_name, we have used the ALIAS clause with AS keyword to shorter the function name and given the name as USING_IF_CONDITION for the output column_name. At the end of the query, the LIMIT clause as LIMIT 10 is used to retrieve the first 10 records of the JOHNS_HOPKINS_HOSPITAL table by using the SELECT statement.

MariaDB if statement in select example
Example of MariaDB IF statement in the SELECT statement

In this section, we hope that you have understood how to use the IF statement in the subtopic “MariaDB If Statement in Select”. We have used an example and explained it in a depth, for better understanding.

Read: MariaDB Select Unique

MariaDB If Statement in Select Count

We will learn and understand how to use the MariaDB IF statement and the COUNT function on the table by the query. And which will be explained with the help of an illustrated example.

The MariaDB COUNT function is used to total the count of an expression or column_name from the query. Here is an illustrated example of the MariaDB IF statement with the COUNT function of the table by the following query:

EXAMPLE:

SELECT IF(FIRST_NAME> LAST_NAME,TRUE,FALSE) AS IF_VALUE, COUNT(ID),FIRST_NAME 
FROM USA_WALMART_COMPANY;

As we see in the above query, the SELECT statement is used to retrieve all records of the FIRST_NAME column from the USA_WALMART_COMPANY table. The IF statement is used with the GREATER THAN operator for checking the values which are greater than in the FIRST_NAME and LAST_NAME columns of the USA_WALMART_COMPANY table.

If the value of the FIRST_NAME column is greater than the LAST_NAME column in the USA_WALMART_COMPANY table then it will return 1 as a TRUE value otherwise vice-versa. We have also used the COUNT function on the ID column which will count the total number of rows in the USA_WALMART_COMPANY table.

MariaDB if statement in select count example
Example of MariaDB IF statement and COUNT function

We hope that you have understood how to use the MariaDB IF statement and COUNT function on the table by the query. For better understanding, we have used an example and explained it in depth.

Read: MariaDB Date Allow Null

MariaDB If Statement In Select Distinct

in this section, we will learn and understand how to use the MariaDB IF statement and DISTINCT function on the table by the query. And we have used ad example and explained it in detail.

The MariaDB DISTINCT function is used to remove duplicate values from the SELECT statement. Here is an illustrated example of the MariaDB IF statement and DISTINCT function on the table by the following query:

EXAMPLE:

SELECT DISTINCT FIRST_NAME, IF(FIRST_NAME> LAST_NAME,TRUE,FALSE) AS IF_VALUE
 FROM usa_walmart_company;

As we see in the above query, we have used the SELECT statement to retrieve all records of the LAST_NAME column from the USA_WALMART_COMPANY table. The IF statement is used with the GREATER THAN operator to find which string value is greater than within the FIRST_NAME and LAST_NAME columns from the USA_WALMART_COMPANY table.

As an output, it will return as 1 for the TRUE value and 0 for the FALSE value if the FIRST_NAME column is less than the LAST_NAME column. To shorter the function name, we have used the ALIAS clause with the AS keyword and given the name IF_VALUE for the output column.

We have also the DISTINCT function on the FIRST_NAME column to find the unique values in the USA_WALMART_COMPANY table.

MariaDB if statement in select distinct example
Example of MariaDB IF statement and DISTINCT function

In this MariaDB tutorial, we hope that you have understood how to use the MariaDB IF statement and DISTINCT function on the table by the query. We have used an example and explained it in depth for better understanding.

Read: MariaDB Select Where Not Empty

MariaDB If Statement In Select Group By

In this section, we will learn and understand how to use the MariaDB IF statement with the GROUP BY clause on the table by the query. And which will be explained with the help of an illustrated example.

The group by clause in MariaDB divides a result’s rows into categories. The aggregate functions count()min(), max(), sum(), and avg() are frequently used with the group by function to obtain attributes of groups.

For example, the number of elements (count), the total of values (sum), the maximum element (max), the minimum element (min), and the average of elements (avg).

Here is an illustrated example of the MariaDB IF statement and GROUP BY clause on the table by the following query:

EXAMPLE:

SELECT first_name, LAST_NAME, IF(first_name> LAST_NAME,TRUE,FALSE) AS IF_VALUE
FROM usa_walmart_company
 GROUP BY first_name;

As we see in the above query, we have used the SELECT statement to retrieve all records of the FIRST_NAME and LAST_NAME columns from the USA_WALMART_COMPANY table. The IF statement is used with the GREATER THAN operator to find which string value is greater than within the FIRST_NAME and LAST_NAME columns from the USA_WALMART_COMPANY table.

As an output, it will return as 1 for the TRUE value and 0 for the FALSE value if the FIRST_NAME column is less than the LAST_NAME column. To shorter the function name, we have used the ALIAS clause with the AS keyword and given the name IF_VALUE for the output column.

At the end of the query, we have used the GROUP BY clause on the FIRST_NAME column to group them based on the SELECT statement used in the USA_WALMART_COMPANY table.

MariaDB if statement in select group by example
Example of MariaDB IF statement used with the GROUP BY clause

We hope that you have understood how to use the MariaDB IF statement and GROUP BY clause on the table. For better understanding, we have used an example and explained it in depth.

Read: MariaDB Date Add Days

MariaDB If Statement In Select Greater Than

In this section, we will learn and understand how to use the MariaDB IF statement with the GREATER THAN operator on the table by the query. And which will be explained with the help of an illustrated example.

The MariaDB GREATER THAN operator is used with the WHERE condition for the records to be selected by using the SELECT statement. It’s come inside the COMPARISON operator.

Here is an illustrated example of the MariaDB IF statement with the GREATER THAN operator on the table by the following query:

EXAMPLE:

SELECT FIRST_NAME, LAST_NAME, IF(FIRST_NAME> LAST_NAME, TRUE, FALSE) AS IF_VALUE
 FROM USA_WALMART_COMPANY
WHERE ID > 5;

As we see in the above query, we have used the SELECT statement to retrieve all records of the FIRST_NAME and LAST_NAME columns from the USA_WALMART_COMPANY table. The IF statement is used with the GREATER THAN operator to find which string value is greater than within the FIRST_NAME and LAST_NAME columns from the USA_WALMART_COMPANY table.

As an output, it will return as 1 for the TRUE value and 0 for the FALSE value if the FIRST_NAME column is less than the LAST_NAME column. To shorter the function name, we have used the ALIAS clause with the AS keyword and given the name IF_VALUE for the output column.

In the WHERE condition, the ID column is used with the GREATER THAN operator to find the value which is greater than 5 from the USA_WALMART_COMPANY table by using the SELECT statement. The SELECT statement will retrieve all records from the USA_WALMART_COMPANY table if the WHERE condition gets TRUE.

By the chance, if the WHERE condition doesn’t get TRUE then it will retrieve empty records from the SELECT statement for the result set.

MariaDB if statement in select greater than example
Example of MariaDB IF statement used with the GREATER THAN operator

We hope that you have understood how to use the MariaDB IF statement and GREATER THAN operator on the table by the query. For better understanding, we have used an example and explained it in depth.

Read: MariaDB Check If Rows Exists

MariaDB If Statement In Select Json

We will here learn and understand how to use the MariaDB IF function with the JSON data type on the table by the query. And which will be explained with the help of an illustrated example.

MariaDB’s JSON data type is a replacement for LONGTEXT, which was designed to work with MySQL’s JSON data type. Because it violates the SQL standard, MariaDB supports the JSON data format as a LONGTEXT instead, and MariaDB’s benchmarks show that efficiency is at least comparable.

SELECT USER_NAME, JSON_VALUE
IF (USER_NAME < JSON_VALUE,TRUE,FALSE) AS IF_VALUE 
FROM JSON_DEMO;

In this preceding query, the SELECT statement is used to retrieve all records of the USER_NAME and JSON_VALUE columns from the JSON_DEMO table. The IF statement is used with the GREATER THAN operator on the JSON_VALUE and USER_NAME columns of the JSON_DEMO table to find greater value within columns.

If the JSON_VALUE column is greater than the USER_NAME column then it will return 1 as a TRUE value. But if the JSON_VALUE column is less than the USER_NAME column then it will return 0 as a FALSE value. As to shorter the function name, we have used the ALIAS clause with the AS keyword and given the name IF_VALUE for the output column name.

MariaDB if statement in select json example
Example of MariaDB IF statement used in the JSON data type

We hope that you have understood how to use the IF function on the JSON data type of the table by the query. For better learning, we have used an example and explained it in depth.

Read: MariaDB Check Constraint + Examples

MariaDB If Statement In Select Like

We will learn and understand how to use the MariaDB IF and LIKE conditions on the table by the query. And which will be explained with the help of an illustrated example.

The LIKE operator is a logical operator in MariaDB that returns TRUE when any string matches the specific pattern provided with the LIKE operator. In other words, with the help of a like operator, we can test whether the string or expression matches the pattern.

If the string or expression matches with the pattern, then it returns TRUE otherwise FALSE. The pattern is used with the LIKE operator, and this pattern is called wildcards. Two wildcards are used with the like operator in MariaDB.

  • Percent (%): It is called a percent wildcard that matches any string with any number of characters.
  • Underscore(_): It is called an underscore wildcard that matches any single character.

Here is an illustrated example of the MariaDB IF and LIKE condition on the table by the following query:

EXAMPLE:

SELECT FIRST_NAME, LAST_NAME, IF(FIRST_NAME> LAST_NAME,TRUE,FALSE) AS IF_VALUE
 FROM USA_WALMART_COMPANY
WHERE FIRST_NAME LIKE '%a';

As we see in the above query, we have used the SELECT statement to retrieve all records of the FIRST_NAME and LAST_NAME columns from the USA_WALMART_COMPANY table. The IF statement is used with the GREATER THAN operator to find which string value is greater than within the FIRST_NAME and LAST_NAME columns from the USA_WALMART_COMPANY table.

As an output, it will return as 1 for the TRUE value and 0 for the FALSE value if the FIRST_NAME column is less than the LAST_NAME column. To shorter the function name, we have used the ALIAS clause with the AS keyword and given the name IF_VALUE for the output column.

In the WHERE condition, the FIRST_NAME is used with the LIKE condition to find the alphabet a whose name ends with alphabet a in the USA_WALMART_COMPANY table by using the SELECT statement.

The SELECT statement will retrieve all records from the USA_WALMART_COMPANY table if the WHERE condition gets TRUE. Due to some reason, if the WHERE condition gets a FALSE value then it will return empty output values from the query.

MariaDB if statement in select like example
Example of MariaDB IF condition used with the LIKE condition

In this section, we have understood how to use the MariaDB IF and LIKE conditions on the table by the query. We have used an example and explained it in detail for better understanding.

Read: MariaDB Greatest Function

MariaDB If Statement In Select Limit

We will learn and understand how to use the MariaDB IF statement and the LIMIT clause of the table by the query. And which will be explained with the help of an illustrated example.

The MariaDB SELECT LIMIT statement retrieves records from one or more tables in MariaDB and limits the number of records returned based on a limit value.

Here is an illustrated example of the MariaDB IF statement and LIMIT clause of the table by the following query:

EXAMPLE:

SELECT FIRST_NAME, LAST_NAME, IF(FIRST_NAME> LAST_NAME,TRUE,FALSE) AS IF_VALUE
 FROM USA_WALMART_COMPANY
LIMIT 10;

As we see in the above query, we have used the SELECT statement to retrieve all records of the FIRST_NAME and LAST_NAME columns from the USA_WALMART_COMPANY table. The IF statement is used with the GREATER THAN operator to find which string value is greater than within the FIRST_NAME and LAST_NAME columns from the USA_WALMART_COMPANY table.

As an output, it will return as 1 for the TRUE value and 0 for the FALSE value if the FIRST_NAME column is less than the LAST_NAME column. To shorter the function name, we have used the ALIAS clause with the AS keyword and given the name IF_VALUE for the output column.

At the end of the query, we have used the LIMIT clause as LIMIT 10 to retrieve the first 10 records of the USA_WALMART_COMPANY table by using the SELECT statement.

MariaDB if statement in select limit example
Example of MariaDB IF statement used with the LIMIT clause

We hope that you have understood the subtopic of “MariaDB If Statement In Select Limit” in the MariaDB tutorial. For better understanding, we have used an example and explained it in depth.

Read: MariaDB Median – Complete Tutorial

MariaDB If Statement In Select Multiple Columns

In this section, we will learn and understand how to use the MariaDB IF statement on the multiple columns of the table by the query. And which will be explained with the help of an illustrated example.

EXAMPLE:

SELECT PATIENT_FIRSTNAME, PATIENT_LASTNAME, 
IF(PATIENT_ADMITDATE < PATIENT_DISCHARGEDATE, TRUE, FALSE) AS IF_VALUE,
IF(PATIENT_FIRSTNAME < PATIENT_LASTNAME, TRUE, FALSE) AS IF_STRING_VALUE
FROM JOHNS_HOPKINS_HOSPITAL 
LIMIT 10;
  • As we see in the above query, the SELECT statement is used to retrieve all records of the PATIENT_FIRSTNAME and PATIENT_LASTNAME columns of the JOHNS_HOPKINS_HOSPITAL table.
  • The IF statement is used with the GREATER THAN operator within the PATIENT_ADMITDATE and PATIENT_DISCHARGEDATE columns to find the greater DATE value of the JOHNS_HOPKINS_HOSPITAL table.
  • If the PATIENT_DISCHARGEDATE column is greater than the PATIENT_ADMITDATE column then it will return 1 as a TRUE value or else vice-versa.
  • The same procedure of the IF statement is used within the PATIENT_FIRSTNAME and PATIENT_LASTNAME columns to find the greater string value in the JOHNS_HOPKINS_HOSPITAL table.
  • If the PATIENT_LASTNAME column is greater than the PATIENT_FIRSTNAME column then it will return 1 as a TRUE value otherwise vice-versa.
  • At the end of the query, we have used the LIMIT clause as LIMIT 10 to retrieve the first 10 records of the JOHNS_HOPKINS_HOSPITAL table by using the SELECT statement.
MariaDB if statement in select multiple columns example
Example of MariaDB IF statement used on the multiple columns

We hope that you have understood the subtopic “MariaDB If Statement In Select Multiple Columns” of the table by the query. For better understanding, we have used an example and explained it in depth.

Read: MariaDB Not Between

MariaDB If Statement In Select Order By

We’ll learn and comprehend how to use the MariaDB IF statement in conjunction with the ORDER BY clause on the query table. And which will be explained via the use of an example.

The MariaDB ORDER BY clause is used to sort the records for the resultset. We haven’t provided the ASC or DESC modifier in the ORDER BY clause then the result set will be sorted the expression in ascending order. This is equal to ORDER BY EXPRESSION ASC.

Let’s see a sample example of the MariaDB IF statement and ORDER BY clause of the table by the following query:

EXAMPLE:

SELECT FIRST_NAME, LAST_NAME, IF(FIRST_NAME> LAST_NAME,TRUE,FALSE) AS IF_VALUE
 FROM USA_WALMART_COMPANY
ORDER BY LAST_NAME DESC;
  • In this preceding query, the MariaDB SELECT statement will retrieve all records of the FIRST_NAME and LAST_NAME columns of the USA_WALMART_COMPANY table.
  • ‘The IF statement is used within the FIRST_NAME and LAST_NAME columns to find the greater string value in them.
  • If the FIRST_NAME column is greater than the LAST_NAME column then in the output it will return 1 as a TRUE value and vice-versa.
  • To shorter the function_name, we have used the ALIAS clause with the AS keyword and given the name IF_VALUE for the output column_name.
  • In the end, we have arranged the LAST_NAME column in descending order by using the ORDER BY clause with the DESC keyword in the SELECT statement.
  • This means that it will first arrange the LAST_NAME column in descending order then by using the IF statement it will give the output for the result set.
MariaDB if statement in select order by example
Example of MariaDB IF statement used with the ORDER BY clause

We hope that you have understood how to use the MariaDB IF statement and ORDER BY clause on the table by the query. For better understanding, we have used an example and explained it in detail.

Read: How to use MariaDB Not Equal Operator

MariaDB If Statement In Select Subquery

In this MariaDB tutorial, we will learn and understand how to use the MariaDB IF statement in the subquery of the SELECT statement of the table by the query. And which will be explained with the help of an illustrated example.

The MariaDB SUBQUERY is a query within the query. We can create subqueries with the SQL statement. These queries reside in the FROM, WHERE, and SELECT clauses.

NOTE:

  • In MariaDB, the subquery is known as INNER SELECT or INNER QUERY.
  • The main query which contains a subquery is known as OUTER QUERY or OUTER SELECT.
SELECT PATIENT_FIRSTNAME,PATIENT_ID, IF(PATIENT_ADMITDATE > PATIENT_DISCHARGEDATE,TRUE, FALSE) AS IF_VALUE
FROM (SELECT * FROM johns_hopkins_hospital
WHERE PATIENT_ID >=10 ) VARIABLE_1 
LIMIT 10;

In the OUTER SELECT query, the SELECT statement is used to retrieve all records of the PATIENT_FIRSTNAME and PATIENT_ID columns from the JOHNS_HOPKINS_HOSPITAL table.

The IF statement is used with the GREATER THAN operator on the PATIENT_ADMITDATE and PATIENT_DISCHARGEDATE columns of the JOHNS_HOPKINS_HOSPITAL table to find the greater value within the columns.

If the PATIENT_DISCHARGEDATE column is greater than the PATIENT_ADMITDATE column then it will return 1 as a TRUE value otherwise it will return 0 as a FALSE value. To shorter the function_name, we have used the ALIAS clause with the AS keyword and given the name IF_VALUE for the output column name.

In the INNER SELECT query, the SELECT statement is used to retrieve all records of the JOHNS_HOPKINS_HOSPITAL table based on the WHERE condition. In the WHERE condition, the PATIENT_ID column is used with the GREATER THAN or EQUAL TO operator to find the value which is greater than or equal to 10 from the JOHNS_HOPKINS_HOSPITAL table.

For the INNER SELECT statement, we have given the name VARIABLE_1. To limit the result set, we have used the LIMIT clause as LIMIT 10 to retrieve the first 10 records from the JOHNS_HOPKINS_HOSPITAL table by using the SELECT statement.

MariaDB if statement in select subquery example
Example of MariaDB If Statment In Select Subquery

In this MariaDB tutorial, we hope that you have understood the subtopic “MariaDB If Statement In Select Subquery”. For a better explanation, we have used an example and explained it in depth.

Read: MariaDB Delete From Statement

MariaDB If Statement In Select Where Condition

In this section, we will learn and understand how to use the MariaDB IF statement in the WHERE condition of the table by the query. And which will be explained with the help of an illustrated example.

In the SELECT query, the MariaDB WHERE condition has been used to refine the results. It can also be used in conjunction with the UPDATE, DELETE and INSERT commands.

Here is an illustrated example of the MariaDB IF statement used with the WHERE condition of the table by the following query:

EXAMPLE:

SELECT PATIENT_FIRSTNAME, PATIENT_LASTNAME, 
IF(PATIENT_ADMITDATE < PATIENT_DISCHARGEDATE, TRUE, FALSE) AS IF_VALUE

FROM johns_hopkins_hospital 
WHERE PATIENT_ID >=30;

In this preceding query, the SELECT statement is used to retrieve all records of the PATIENT_FIRSTNAME and PATIENT_LASTNAME columns from the JOHNS_HOPKINS_HOSPITAL table. The IF function is used with the GREATER THAN operator within the PATIENT_ADMITDATE and PATIENT_DISCHARGEDATE columns of the JOHNS_HOPKINS_HOSPITAL table to find the greater value between them.

If the PATIENT_DISCHARGEDATE column is greater than the PATIENT_ADMITDATE column then it will return 1 as a TRUE value otherwise vice-versa. To shorter the function_name, we have used the ALIAS clause with the AS keyword and given the name IF_VALUE for the output column_name.

In the WHERE condition, the PATIENT_ID column is used with the GREATER THAN or EQUAL TO operator to find the value which is greater than or equal to 30 in the JOHNS_HOPKINS_HOSPITAL table. The SELECT statement is used to retrieve all records from the JOHNS_HOPKINS_HOSPITAL table if the WHERE condition gets TRUE.

If the WHERE condition gets TRUE or FALSE based on the query, it will still filter out the results from the JOHNS_HOPKINS_HOSPITAL table. But if the WHERE condition gets FALSE then the SELECT statement will still retrieve all records from the JOHNS_HOPKINS_HOSPITAL table.

MariaDB if statement in select where condition example
Example of MariaDB IF statement used with the WHERE condition

We hope that you have understood how to use the MariaDB IF statement on the WHERE condition of the table by the query. For better knowledge, we have used an example and explained it in depth.

Read: MariaDB JSON Data Type

MariaDB If Statement In Select Yesterday

In this section, we will learn and understand how to use the MariaDB IF statement, CURRENT_DATE, and DATE_SUB function for the yesterday value of the table by the query. And which will be explained with the help of an illustrated example.

The MariaDB DATE_SUB function is used to return the DATE value after which the date/time interval has been subtracted. Another method as CURRENT_DATE function is used to extract the current date and time value.

Let’s see an illustrated example of the MariaDB IF statement with the CURRENT_DATE and DATE_SUB function to find the YESTERDAY value of the table by the query:

EXAMPLE:

SELECT PATIENT_FIRSTNAME, PATIENT_LASTNAME, 
IF(PATIENT_ADMITDATE < PATIENT_DISCHARGEDATE, TRUE, FALSE) AS IF_VALUE, 
DATE_SUB(CURRENT_DATE, INTERVAL 1 DAY) AS YERSTERDAY_VALUE
FROM JOHNS_HOPKINS_HOSPITAL 
LIMIT 10;

In this preceding query, the SELECT statement is used to retrieve all records of the PATIENT_FIRSTNAME and PATIENT_LASTNAME columns from the JOHNS_HOPKINS_HOSPITAL table. The IF function is used with the GREATER THAN operator within the PATIENT_ADMITDATE and PATIENT_DISCHARGEDATE columns of the JOHNS_HOPKINS_HOSPITAL table to find the greater value between them.

If the PATIENT_DISCHARGEDATE column is greater than the PATIENT_ADMITDATE column then it will return 1 as a TRUE value otherwise vice-versa. To shorter the function_name, we have used the ALIAS clause with the AS keyword and given the name IF_VALUE for the output column_name.

The DATE_SUB function is used with the CURRENT_DATE column to find the yesterday value with the INTERVAL keyword. We have also used the ALIAS clause with the AS keyword and given the name YESTERDAY_VALUE for the output column_name.

To shorter the output result set at the end of the query, we have used the LIMIT clause as LIMIT 10 to retrieve the first 10 records of the JOHNS_HOPKINS_HOSPITAL table by using the SELECT statement.

MariaDB if statement in select yesterday example
Example of MariaDB If Statement In Select Yesterday

We hope that you have to understand the concept of the subtopic “MariaDB If Statement In Select Yesterday”. For better understanding, we have used an example and explained it in depth.

Read: MariaDB Max Connections

MariaDB If Statement In Select Year

In this MariaDB tutorial, we will learn and understand how to use the MariaDB IF statement with the YEAR function on the table by the query. And which will be explained with the help of an illustrated example.

The MariaDB YEAR function is used to extract the year portion value from the expression or column_name of the query. Here is a sample example of the MariaDB IF statement with the YEAR function on the table by the following query:

EXAMPLE:

SELECT PATIENT_FIRSTNAME, PATIENT_LASTNAME, YEAR(PATIENT_ADMITDATE),
IF(PATIENT_ADMITDATE < PATIENT_DISCHARGEDATE, TRUE, FALSE) AS IF_VALUE
FROM JOHNS_HOPKINS_HOSPITAL
LIMIT 10;

As we see in the above query, the SELECT statement is used to retrieve all records of the PATEINT_FIRSTNAME and PATIENT_LASTNAME columns from the JOHNS_HOPKINS_HOSPITAL table. The YEAR function is used to extract the year portion value from the PATIENT_ADMITDATE column from the JOHNS_HOPKINS_HOSPITAL table.

The IF statement is used with the GREATER THAN operator within the PATIENT_ADMITDATE and PATIENT_DISCHARGEDATE columns to find the greater DATE value from the JOHNS_HOPKINS_HOSPITAL table. If the PATIENT_ADMITDATE column is less than the PATEINT_DISCHARGEDATE column then it will return 0 as a FALSE value else vice-versa.

To shorter the function_name, we have used the ALIAS clause with the AS keyword and given the name IF_VALUE for the output column_name. At the end of the query, the LIMIT clause is used as LIMIT 10 to retrieve the first 10 records of the JOHNS_HOPKINS_HOSPITAL table by using the SELECT statement.

MariaDB if statement in select year example
Example of MariaDB IF statement used with the YEAR function

We hope that you have understood the subtopic “MariaDB If Statement In Select Year” by the above query. For a better explanation, we have used an example and explained it in detail.

Also, take a look at some more MariaDB tutorials.

In this MariaDB lesson, we covered the use of the MariaDB If Statement In Select statement and various sample instances related to it. There are several listings of the topics that are discussed:

  • MariaDB If Statement In Select
  • MariaDB If Statement In Select Count
  • MariaDB If Statement In Select Distinct
  • MariaDB If Statement In Select Group By
  • MariaDB If Statement In Select Greater Than
  • MariaDB If Statement In Select Json
  • MariaDB If Statement In Select Like
  • MariaDB If Statement In Select Limit
  • MariaDB If Statement In Select Multiple Columns
  • MariaDB If Statement In Select Order By
  • MariaDB If Statement In Select Subquery
  • MariaDB If Statement In Select Where Condition
  • MariaDB If Statement In Select Yesterday
  • MariaDB If Statement In Select Year