데이터의 민족

< MySQL 기본 > 본문

DB의 민족/MYSQL

< MySQL 기본 >

댕구리댕댕구리 2022. 3. 28. 19:50
728x90
반응형
SMALL

어떤 테이블이 내장되어 있는 지 확인

show databases;

 

사용할 테이블 설정

use sakila;

 

테이블 안의 내용을 확인

describe actor;

 

< 구조 >

  • SELECT
    • 테이블에서 필요한 열만 추출
    • 여러 개의 열을 가져오려면 ' , ' 로 구분
select actor_id, first_name 
from actor;

 

  •  WHERE
    • 조회하는 결과에 특정한 조건을 원하는 데이터만 보고 싶은 경우
select actor_id 
from actor 
where actor_id = 57;

 

 

  • 관계 연산자
    • BETWEEN : 데이터가 숫자로 구성이 되어 있어 연속적인 값 
    • IN : 이산적인 값에서 사용
select * from actor 
where actor_id between 40 and 50;

select * from actor
where first_name in('JOHN');

 

  • LIKE
    • 문자열의 내용 검색하기 위해서는 Like 사용
    • 문자 뒤에 %- 무엇이든% 허용
    • 한 글자, 매치는 _
select * from actor 
where first_name Like 'J%';

 

  • 서브쿼리
select * from actor 
where first_name = (select first_name from actor where actor_id = 3);

 

 

  • SOME, ANY
    • 서브쿼리의 조건 하나만 만족해도 가능
select * from city 
where Population > ANY (select Population from city where District = 'New York');

 

 

  • ALL
    • 서브쿼리 여러 개의 결과를 모두 만족
select * from city 
where Population > All (select Population from city where District = 'New York');

 

  • ORDER BY
    • 결과가 출력되는 순서조절
    • defaul는 ASCENDING(오름차순), DESC(내림차순)
select * from city 
order by 1; 

select * from city 
order by Population DESC;

 

 

  • DISTINCT
    • 중복 제외
select distinct  CountryCode
from city;

 

  • LIMIT
    • 출력 개수를 제한 / 서버의 처리량을 많이 사용해 악성 쿼리문 개선 시 유용
select *
from city 
order by Population DESC
limit 8;

 

  • GROUP BY
    • 그룹으로 묶어주는 역할
    • 읽기 좋게 별칭으로 사용
select CountryCode , max(Population) as 'Pop' 
from city
group by CountryCode;

 

  • HAVING
    • WHERE과 비슷한 조건 
    • 반드시 GROUP BY 뒤에 나옴
 select * from city
 group by CountryCode
 having Max(Population) > 100000;

 

  • ROLLUP
    • 중간 합계가 필요한 경우
    • GROUP BY 절과 함께 WITH ROLLUP구문 사용
select CountryCode, Name, MAX(Population)
from city
group by CountryCode, Name WITH ROLLUP;

 

  • JOIN
    • 데이터 베이스 내의 여러 테이블에서 가져온 레코드를 조합해 하나의 테이블 결과 출력
select *
from city c1 join country c2 
on c1.CountryCode = c2.Code;

 

728x90
반응형
LIST

'DB의 민족 > MYSQL' 카테고리의 다른 글

< MySQL 다운로드 >  (0) 2022.08.17
< MySQL DDL(데이터 정의어) >  (0) 2022.04.05
< MySQL 내장 함수 >  (0) 2022.04.05
< MySQL 개념 >  (0) 2022.03.25
Comments