Mariadb Or Condition

MariaDB: OR Condition

This MariaDB tutorial explains how to use the MariaDB OR condition with syntax and examples.

Description

The MariaDB OR condition is used to test two or more conditions where records are returned when any one of the conditions are met. The OR condition can be used in a SELECT, INSERT, UPDATE, or DELETE statement.

Syntax

The syntax for the OR condition in MariaDB is:

WHERE condition1
OR condition2
...
OR condition_n;

Parameters or Arguments

condition1, condition2, ... condition_n

Any of the conditions that must be met for the records to be selected.

Note

  • The MariaDB OR condition allows you to test 2 or more conditions.
  • The MariaDB OR condition requires that any of the conditions (ie: condition1, condition2, condition_n) be must be met for the record to be included in the result set.

Example - With SELECT Statement

Let's look at how to use the OR condition in the SELECT statement in MariaDB.

For example:

SELECT *
FROM sites
WHERE site_name = 'mySite.com'
OR site_id = 2;

This MariaDB OR condition example return all sites that have a site_name of 'mySite.com' or a site_id that is equal to 2. Because the * is used in the SELECT statement, all fields from the sites table would appear in the result set.

Example - With SELECT Statement (3 conditions)

Let's look at how to use the OR condition to test for three conditions in the SELECT statement in MariaDB.

For example:

SELECT site_id, site_name
FROM sites
WHERE site_name = 'mySite.com'
OR site_name = 'AODBA.com'
OR site_id = 2;

This OR condition example would return all site_id and site_name values where the site_name is either 'mySite.com' or 'AODBA.com' or the site_id is equal to 2.

Example - With INSERT Statement

Let's look at how to use the OR condition in the INSERT statement in MariaDB.

For example:

INSERT INTO contacts
(contact_id, contact_name)
SELECT site_id, site_name
FROM sites
WHERE site_name = 'AODBA.com'
OR site_id  1000;

This MariaDB OR example would insert into the contacts table, all site_id and site_name records from the sites table that have a site_name of 'AODBA.com' or the site_id is less than 1000.

Example - With UPDATE Statement

Let's look at how to use the OR condition in the UPDATE statement in MariaDB.

For example:

UPDATE sites
SET site_name = 'AODBA.com'
WHERE site_id = 10
OR site_name = 'data.com';

This OR condition example would update all site_name values in the sites table to 'AODBA.com' where the site_id is equal to 10 or the site_name is 'data.com'.

Example - With DELETE Statement

Finally, let's look at how to use the OR condition in the DELETE statement in MariaDB.

For example:

DELETE FROM sites
WHERE site_name = 'AODBA.com'
OR site_id = 65;

This MariaDB OR example would delete all records from the sites table where the site_name is 'AODBA.com' or the site_id is equal to 65.