The SQL AND Operator – SQL-ын AND оператор #

The WHERE clause can contain one or many AND operators.

WHERE нөхцөл нь нэг болон нэгээс их AND оператор ашигладаг.

The AND operator is used to filter records based on more than one condition, like if you want to return all customers from Spain that starts with the letter ‘G’:

AND оператор нь нэгээс олон нөхцөл дээр үндэслэн мэдээллүүдээс шүүдэг, жишээлбэл ‘G’ үсгээр эхэлсэн Испани гаралтай бүх хэрэглэгчдийн харах:

Example – Жишээ #

Select all customers from Spain that starts with the letter ‘G’:

‘G’ үсгээр эхэлсэн Испани гаралтай бүх хэрэглэгчдийг сонгох:

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

Syntax – Дүрэм #

SELECTcolumn1, column2, ...
FROMtable_name
WHEREcondition1ANDcondition2ANDcondition3 ...;

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

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

AND операторт оруулсан бүх нөхцөл ҮНЭН байх ёстой.

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

OR операторт оруулсан нэг нөхцөл л ҮНЭН байхад болно.


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

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

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

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

All Conditions Must Be True – Бүх Нөхцлүүд Заавал Үнэн байх ёстой #

The following SQL statement selects all fields from Customers where Country is “Germany” AND City is “Berlin” AND PostalCode is higher than 12000:

Доорх SQL жишээнд бүх Хэрэглэгчдийг сонгож авч Улс нь “Герман” БАС(AND)  Хотнь “Берлин” БАС(AND) ПосталКоднь 12000 их хэрэглэгчүүдийг хайсан:

Example – Жишээ #

SELECT * FROM Customers
WHERE Country = ‘Germany’
AND City = ‘Berlin’
AND PostalCode > 12000;

 

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