본문 바로가기
번역/MariaDB

[MariaDB Tutorial 번역] 3. Where

by 촌쥐 2021. 6. 24.

[MariaDB Tutorial 번역 글]

  1. Select : https://rural-mouse.tistory.com/43
  2. Order By : https://rural-mouse.tistory.com/44
  3. Where ←←
  4. Distinct : https://rural-mouse.tistory.com/46
  5. Between : https://rural-mouse.tistory.com/48
  6. Like : https://rural-mouse.tistory.com/49
  7. In : https://rural-mouse.tistory.com/50
  8. Limit : https://rural-mouse.tistory.com/51
  9. Is Null : https://rural-mouse.tistory.com/52
  10. Joins : https://rural-mouse.tistory.com/53
  11. Inner Join : https://rural-mouse.tistory.com/54
  12. Left Join : https://rural-mouse.tistory.com/55
  13. Group By : https://rural-mouse.tistory.com/56
  14. Having : https://rural-mouse.tistory.com/59
  15. Subqueries : https://rural-mouse.tistory.com/60
  16. Insert : https://rural-mouse.tistory.com/61
  17. Insert Multiple Rows : https://rural-mouse.tistory.com/62
  18. Insert Into Select : https://rural-mouse.tistory.com/63
  19. Update : https://rural-mouse.tistory.com/64

원문 :https://www.mariadbtutorial.com/mariadb-basics/mariadb-where/

 

MariaDB Where

In this tutorial, you will learn how to use the MariaDB where clause to filter rows returned by a query.

www.mariadbtutorial.com


여기에 있는 모든 예제는 MariaDB sample database인 nation을 가지고 진행됩니다.

만약 읽고 따라 해 보시는 게 목적이라면 다운로드를 받고 읽어보시길 추천합니다.

원본 링크 : https://www.mariadbtutorial.com/getting-started/mariadb-sample-database/

 

MariaDB Sample Database

In this tutorial, you will learn about a MariaDB sample database nation and how to load the sample database into the MariaDB server.

www.mariadbtutorial.com


MariaDB Where 명령에 대한 소개

Where 구문은 Select 명령의 행을 선택하기 위한 검색 조건을 특정하는 옵션입니다.

다음은 Select 명령에 Where 절을 사용한 예시입니다.

select
    select_list
from
    table_name
where
    search_condition
order by
    sort_expression;

이 문법에서, 저희는 Table에서 row을 선택하기 위한 검색 조건을 특정하였습니다.

만약 row가 search_condition으로 계산하였을 때 True가 된다면 Result set에 포함됩니다.

검색 조건은 하나 이상의 논리적인 표현식으로 구성될 수 있습니다. 논리적인 표현식은 항상 계산 후 True, False, Unknown 중 하나의 결과를 나타냅니다. 논리적 표현식은 predicate라고 쓰이기도 합니다.

Select 명령에서 Where 절은 From 절 이후에 Order By 절 이전에 쓰입니다.

MariaDB는 Select, From, Where, Order By 가 포함된 Select 명령을 수행할때 From, Where, Select, Order By 순서대로 시행됩니다.

Query 에서의 실행 순서

 

MariaDB의 Where 구문에 대한 예제

Sample db인 nation에 있는 countries Table을 가지고 시행해보겠습니다. 

A) MariaDB의 equal( = )과 함께 쓰이는 Where 구문 예제

다음 예제는 region_id가 2인 countries를 선택하는 구문입니다.

select 
    name, 
    area, 
    region_id
from 
    countries
where 
    region_id = 2
order by
    name;

Where 구문의 결과

B) MariaDB의 비교 연산자와 함께 쓰이는 Where 구문

다음 Where 구문은 area가 2,000,000 km2 이상인 countries를 찾는 구문입니다.

select 
    name, 
    area
from 
    countries
where 
    area > 2000000
order by 
    area;

Where 구문의 결과

C) MariaDB의 and 연산자와 함께 쓰이는 Where 구문

and 연산자는 boolean 표현식을 포함하며 오직 두 개의 표현식이 둘 다 True일 때만 True를 반환합니다.

다음 예제는 region_id가 1이고 area가 2,000,000 km2 이상인 countrie를 검색하는 Where 구문입니다. 

select 
    name, 
    area, 
    region_id
from 
    countries
where 
    region_id = 2 and 
    area > 2000000
order by 
    name;

where 구문의 결과

 

D) MariaDB의 or 연산자와 함께 쓰이는 Where 구문

or 연산자는 boolean 표현식을 포함하며 두 개의 표현식이 둘 중에 하나라도 True라면 True를 반환합니다.

다음 예제는 region_id가 2이거나 area가 2,000,000 km2 이상인 countrie를 검색하는 Where 구문입니다. 

select 
    name, 
    area, 
    region_id
from 
    countries
where 
    region_id = 2 or 
    area > 2000000
order by 
    name;

Where 구문의 결과

E) MariaDB의 두 개의 값 사이의 값들을 찾아내는 Where 구문

between 연산자는 두개의 값 사이의 값이라면 True를 반환하는 연산자입니다.

expression between low and high

위의 예제는 아래 구문의 숏컷입니다.

expression >= low and expression <= high

예를 들면 이 between의 연산자를 사용한 명령은 1,001,449 와 1,566,500 km2 사이의 countries를 찾는 구문입니다.

select 
    name, 
    area
from 
    countries
where 
    area between 1001449 
        and 1566500
order by 
    area;

Where 구문의 결과

F) MariaDB의 리스트 안의 값들을 찾아내는 Where 구문

리스트 안의 값들중에 값이 있는지 확인하고 싶다면 in 연산자를 사용하면 됩니다.

expression in (value1, value2,...)

in 연산자는 값이 list 안의 값 value1, value2, … 들 중 하나라면 True를 반환해줍니다.

다음 Where 구문 안의 in 은 country code가 US, FR, JP country code 중에 하나인 country를 찾아내는 명령어입니다,

select 
    name, 
    country_code2
from 
    countries
where 
    country_code2 in ('US','FR','JP')
order by 
    name;

in 구문의 결과

 

G) MariaDB의 패턴에 기반하여 찾아내는 Where 구문

Like 연산자는 지정한 패턴과 일치한다면 True를 반환합니다.

expression LIKE pattern;

 

  • % 는 하나 이상의 글자와 매칭됩니다.
  • _ 은 하나의 글자와만 매칭됩니다.

다음의 like 연산자가 포함된 Where 구문의 예제는 시작 글자가 J로 시작하는 name을 가진 country를 찾습니다.

select 
    name
from 
    countries
where 
    name like 'J%'
order by 
    name;

Like 구문의 결과

 

이번 튜토리얼에서는 MariaDB의 Where 를 사용하여 검색 조건에 따라 row를 검색하는 방법을 배웠습니다.

다음에는 중복되는 값들을 제거해주는 옵션인 Distinct에 대해 배워보겠습니다. 

'번역 > MariaDB' 카테고리의 다른 글

[MariaDB Tutorial 번역] 6. Like  (0) 2021.06.29
[MariaDB Tutorial 번역] 5. Between  (0) 2021.06.27
[MariaDB Tutorial 번역] 4. Distinct  (0) 2021.06.27
[MariaDB Tutorial 번역] 2. Order By  (0) 2021.06.24
[MariaDB Tutorial 번역] 1. Select  (0) 2021.06.23

댓글