qordpsem 2024. 6. 11. 13:31

#like 연산자

문자열의 어떠한 패턴을 만족하는 데이터 조회할때 사용

%   :   0개 이상 아무 글자

-     :   1개의 아무 글자

 

#데이터 조회

select 컬럼이름(들) from 테이블이름(들) where 조건식 order by 컬럼(들)

 

#오름차순

order by 컬럼이름 asc

 

#내림차순

order by 컬럼이름 desc

 

 

#DB 명령 종류

DDL     :      데이터 정의어

DCL     :      데이터 제어어

DML     :      데이터 조작어

 

 

 

 

ex)도서명에 '축구'를 포함하고있는 모든 도서 출력

select * from book where bookname like '%축구%';

ex) 도서명이 '축구'로 시작하는 도서 정보 출력

select * from book where bookname like '축구%';

ex) 도서명이 '축구'로 끝나는 도서 정보 출력

select * from book where bookname like '%축구';

 

 

ex)성이 김씨인 모든 고객 정보 출력

select * from customer where name like '김%';

ex)성이 김씨고 성 포함 이름 2글자인 고객 정보 출력

select * from customer where name like '김_';

ex) 성이 김씨고 성 포함 이름 3글장니 고객 정보 출력

select * from customer where name like '김__';

ex) 성이 김씨고 서울에 거주하는 고객

select * from customer where name like '김%' and address like '%서울%';

 

ex) 도서 정보 가격순 정렬 출력

select * from book order by price asc;

ex) 도서명 두번째 글자가 '구', 가격이 5000~50000 사이인 도서 정보 출력 (단, 출판사순, 출판사 동일 시 가격순)

select * from book where bookname like '_구%' and price between 5000 and 50000 order by publisher, price;

 

 

ex) 전화번호가 '000-6000-0001' 인 고객 이름과 주소 출력

select name, address from customer where phone = '000-6000-0001';

ex) 고객번호가 1번인 고객의 주문내역 출력

select * from orders where custid = 1;

ex) 도서번호 10번 or 8번 or 3번 도서의 주문내역을 최근 주문일 순서대로 출력

select from orders where bookid in (10,8,3) order by orderdate desc;

ex) 야구 관련 도서의 도서정보 출력 (단, 가격 높은순으로 출력, 가격 동일 시 도서명 순)

select * from books where bookname like '%야구%' order by price desc, bookname;

ex) '2024/06/01' 과 '2024/06/10' 사이에 판매된 판매가격이 20000원 이상인 판매내역을 출력 (단, 최근의 판매내역부터 출력, 판매일이 동일 시 판매가격 높은순으로 출력)

select * from orders where oderdate between '2024/06/01' and '2024/06/10' and saleprice >=20000 order by oderdate desc, saleprice desc;

 

 

 

#조회하고자하는 컬럼이나 조건식이 두개이상 테이블에 있을때 "조인" 사용

select 컬럼(들) from 테이블1, 테이블2 where 조인 식;

 

조인 식  =>  두개의 테이블에 공동으로 들어가는 컬럼 씀

 

ex) 10번 도서 구매한 고객 이름 출력

select name from customer, orders where customer.custid = orderes.custid and bookid = 10;

ex) 1번 고객이 구매한 도서명 검색

select bookname from book, orders where book.bookid = orders.bookid and custid = 1;

 

#테이블 이름 붙여주기

select bookname from book b, orders o where b.bookid = o.bookid and custid = 1;

 


 

select bookid, bookname, price, saleprice, orderdate, publisher
	from book b, orders o
	where b.bookid = o.bookid and
	custid = 1;

*이렇게 하면 첫번째 줄 bookid 때문에 오류 남

 

bookid 가 book 테이블, orders 테이블 둘 다 있기 때문에 둘 중 아무 테이블에 지정 해줘야함

 

//select o.bookid, bookname, price, saleprice, orderdate, publisher
select b.bookid, bookname, price, saleprice, orderdate, publisher
	from book b, orders o
	where b.bookid = o.bookid and 
	custid = 1;

 

 

 

 

 

 

 

ex) 이상미디어'나 '대한미디어'의 '축구'나 '야구' 관련 도서중에  가격이 10000원 이상인 도서를 구매한 고객의 
고객번호, 고객이름, 도서번호, 도서명, 구매일을 출력
(단, 최근의 구매일 순으로 출력하고 동일할 때에는 도서번호순으로 출력)

select c.custid, name, b.bookid, bookname, orderdate 
from customer c, orders o, book b
where  c.custid = o.custid and 
b.bookid = o.bookid and 
publisher in ('이상미디어','대한미디어') and 
( bookname like '%축구%' or bookname like '%야구%' ) and 
price >= 10000
order by orderdate desc, b.bookid;