Code | Name | Continent | Population | isTraveled |
---|---|---|---|---|
KOR | South Korea | Asia | 48000000 | YES |
JAP | Japan | Asia | 100000000 | YES |
CAN | Canada | North America | 38000000 | |
BRA | Brazil | South America | 210000000 | |
FRA | France | Europe | 65000000 | YES |
DEU | Germany | Europe | 83000000 |
select (나타낼 데이터)
from (탐색할 테이블 명)
where (조건);
=
select *
from country
where Code = 'KOR';
==
으로 비교하지 않는다distinct
select distinct Continent
from country;
distinct
사용concat
select concat(Name, '은 ', Continent, '입니다')
from country;
concat
함수를 사용해 준다면 프로그래밍 언어에서 사용하는 보통의 출력방식을 따라간다order by
order by
다음은 어떤 데이터들을 기준으로 잡고 정렬할지 써준다select *
from country
order by Population;
desc
추가select *
from country
order by Population desc;
order by
뒤에 comma
로 구분하여 작성select *
from country
order by Population, Code;
ifnull
select Code, Name, ifnull(isTraveled, 'NO') as '여행경험'
from country;
isTraveled
의 데이터가 null
이라면 두번째 인자값인 NO
를 출력해라는 뜻ifnull
을 사용하지 않는다면, (data) is null
또는 (data) is not null
과 같은 구문을 사용하여 판별해야 한다select Code, Name, Population
from country
where Population < 100000000 and isTraveled = 'YES';
char_length
length
length
함수는 문자의 Byte
길이를 가져오기 때문에 한글의 정확한 길이를 알 수 없음char_length
는 문자의 Byte
수를 계산하지 않고 단순히 몇 개의 문자가 있는지 가져오는 함수limit
select *
from country
order by Population
limit 3;
limit
를 사용하여 최상위에서부터 갯수를 잘라 출력할 수 있다limit a,b
select *
from country
limit 0, 2;
limit
뒤에 comma
로 구분하면 첫번째 값 만큼 skip
하고, 두번째 값의 만큼을 출력할 것인지 정하는것 like
select *
from country
where Name like '%Kor%';
like
로 해당 문자를 포함하고 있는지 확인%
: 어떤 값이던 몇개나 있던 상관이 없음_
: 어떤 값이던 한개만 있어야 하고, 여러개 사용 가능data%
_data
%data
%data%
와 같이 사용in
select *
from country
where Continent in ('Asia', 'Europe');
in
뒤에있는 단어들이 포함되어 있는지 확인*
로 출력하기select Code,
concat(left(Name, 2),
lpad('*', char_length(name) - 4, '*'),
right(name, 2)) change
from country;
left
와 right
로 양쪽 두개는 그대로 출력lpad
로 *
를 원하는만큼 채우기concat
을 통해 합쳐서 출력case
when
then
end
select Code, Name,
case when Population >= 100000000 then '많다'
else '적다'
end '인구가?'
from country;
case
로 시작해서 end
로 종료시켜 주어야 함round
select Code, Name, round(Population, 1) '소수점출력'
from country;
round
함수를 사용하여 첫번째 파라미터에는 변환할 값, 두번째 파라미터에는 소수점 몇번째 자리까지 나타낼 것인지