The SQL OR Operator – SQL OR оператор #

The WHERE clause can contain one or more OR operators.

WHERE нөхцөлд OR операторуудыг нэг болон нэгээс олон бичиж болно.

The OR operator is used to filter records based on more than one condition, like if you want to return all customers from Germany but also those from Spain:

OR оператор нь нэгээс олон нөхцөл дээр үндэслэн мэдээллүүдийг шүүхэд ашиглагддаг, жишээлбэл та Германаас бүх харилцагчдыг, мөн Испаниас бүх харилцагчдыг буцаахыг хүсвэл:

Example – Жишээ #

Select all customers from Germany or Spain:

Германаас эсвэл Испаниас бүх харилцагчдыг сонгоно уу:

SELECT *
FROM Customers
WHERE Country = ‘Germany’ OR Country = ‘Spain’;

Syntax – Дүрэм #

SELECTcolumn1, column2, ...
FROMtable_name
WHEREcondition1ORcondition2ORcondition3 ...;


OR vs AND – OR болон AND #

The OR operator displays a record if any of the conditions are TRUE.

OR оператор нь аль нэг нөхцөл үнэн бол мэдээллийг харуулдаг.

The AND operator displays a record if all the conditions are TRUE.

AND оператор нь бүх нөхцөл үнэн бол мэдээллийг харуулдаг.


Demo Database – Жишээ мэдээллийн сан #

Below is a selection from the Customers table used in the examples:

Доорх Customers  хүснэгтийг жишээ болгож ашиглав:

CustomerID CustomerName ContactName Address City PostalCode Country
1 Alfreds Futterkiste Maria Anders Obere Str. 57 Berlin 12209 Germany
2 Ana Trujillo Emparedados y helados Ana Trujillo Avda. de la Constitución 2222 México D.F. 05021 Mexico
3 Antonio Moreno Taquería Antonio Moreno Mataderos 2312 México D.F. 05023 Mexico
4 Around the Horn Thomas Hardy 120 Hanover Sq. London WA1 1DP UK
5 Berglunds snabbköp Christina Berglund Berguvsvägen 8 Luleå S-958 22 Sweden

At Least One Condition Must Be True – Хамгийн бага даа нэг нөхцөл заавал үнэн байх #

The following SQL statement selects all fields from Customers where either City is “Berlin”, CustomerName starts with the letter “G” or Country is “Norway”:

Доорх SQL жишээ нь Хот(City) нь “Берлин”, эсвэл ХарилцагчийнНэр(CustomerName) нь “G” үсгээр эхэлдэг, эсвэл Улс(Country) нь “Норвеги” гэсэн бүх Харилцагчдыг хүснэгтээс сонгоно:

Example – Жишээ #

SELECT * FROM Customers
WHERE City = ‘Berlin’OR CustomerName LIKE ‘G%’OR Country = ‘Norway’;

  #

Combining AND and OR – AND болон OR хамт хэрэглэх #

You can combine the AND and OR operators.

AND болон OR операторуудыг хамт хэрэглэж болно.

The following SQL statement selects all customers from Spain that starts with a “G” or an “R”.

Доорх SQL жишээнд бүх харилцагчдаас Испани гаралтай “G” эсвэл “R” үсгээр эхэлснийг нь сонгоно.

Make sure you use parenthesis to get the correct result.

Дугуй хаалт ашиглаж зөв үр дүн авна уу.

Example – Жишээ #

Select all Spanish customers that starts with either “G” or “R”:

Испани гаралтай “G” эсвэл “R” үсгээр эхэлсэн бүх харилцагчдийг сонгон уу: 

SELECT * FROM Customers
WHERE Country = ‘Spain’AND (CustomerName LIKE ‘G%’ OR CustomerName LIKE ‘R%’);

Without parenthesis, the select statement will return all customers from Spain that starts with a “G”, plus all customers that starts with an “R”, regardless of the country value:

Харин дугуй хаалтгүйгээр, Испани гаралтай “G” үсгээр эхэлсэн бүх харилцагчдыг сонгохоос гадна, бусад бүх улсын “R” үсгээр эхэлсэн харилцагчдыг сонгоно уу:

Example – Жишээ #

Select all customers that either:
Бүх харилцагчдыг сонгоно уу:
are from Spain and starts with either “G”, or
тэд Испани гаралтай “G” үсгээр эхэлсэн нэртэй байх, эсвэл
starts with the letter “R”:
бусад улсаас “R” үсгээр эхэлсэн нэртэй байх: 

SELECT * FROM Customers
WHERE Country = ‘Spain’ AND CustomerName LIKE ‘G%’ OR CustomerName LIKE ‘R%’;

Powered by BetterDocs

Leave a Reply