본문 바로가기
[DBMS]/- Oracle

[Oracle]테이블 조회 ( select)

by Hapco 2022. 5. 29.
728x90
반응형

--조회구문의형식

--selcet from 대상(테이블);

--product 테이블의 모든항목(*)을 조회

select * from product;

--product 테이블을 이름(name)으로 조회

select name from product;

--product 테이블의 이름과 가격을조회

select name,price from product;

--product 테이블의 name,price에 별칭을 부여조회

select name 이름,price 가격 from product;

select name "이름 이름",price "판매 가격" from product;

/*

행 필터링 조건

=데이터베이스에 10만개의 데이터가 잇을 경우 다 보는 경우보다는 " 목적 " 에맞게 선택적으로 보는 것이 중요

=select 항목 from 대상 where 필터식;

*/

--가격이 1000원 상품을 조회

select * from product where price=1000;

--번호가 7번인 상품을 조회

select * from product where no=7;

--가격이 1000원 상품을 조회

select * from product where price=1000;

--가격이 2000원 미만인 상품을 조회

select * from product where price<2000;

--가격이 1000원이상 2000원 이하인 상품을 조회

-- a이상 b이하 라고 표현할 수 있지만 a와 b사이처럼 표현도 가능하다.

select * from product where 1000<=price and price<=2000;

select * from product where price between 1000 and 2000;

--가격이 1000원이 아닌 상품을 조회

select * from product where price !=1000;

--select * from product where price <>1000;

/*

문자열 조건

오라클에서는 문자열도 숫자처럼 비교가 가능하다.

*/

--이름이 스크류바인 상품을 조회

select * from product where name='스크류바';

--아이스크림만 조회;

select * from product where type='아이스크림';

--유사 검색 : 일부분만으로 검색하는 방법(instr 명령사용 / like 연산자 사용)\

--llke 에서는 %를 있어도 그만 없어도 그만 으로 처리한다

--= 시작한다는 의미는 뒤에 %를 붙이면된다

--= 종료한다는 의미는 앞에 %을 붙이면된다.

--= 포함한다는 의미는 앞뒤에 %를 붙이면도;ㅣㄴ다

--instr()에서는 문자열의 시작점을 1로본다.

-- =시작한다는 의미는 instr()결과가 1이란 소리이다.

-- =종료한다는 의미는 instr()결과가 (항목글자수-검색어글자수+1)이란 소리이다.

-- =포함된다는 의미는 inset()결과가 0보다 크다는 소리이다.

select instr(name,'바')from product;

-- 이름이 '바'로 시작하는 상품 정보를 조회

select * from product where name like '바%';

select * from product where instr(name,'바')=1;

select * from product where regexp_like(name,'바[가-힣].*?바');

-- 이름이 '바'로 끝나는 상품 정보를 조회

select * from product where name like '%바';

select * from product where instr(name,'바')=length(name)-length('바')+1;

-- 이름에 '바'가 포함된 상품 정보를 조회

select * from product where name like '%바%';

select * from product where instr(name,'바')>0;

select * from product where regexp_like(name,'[가-힣]*바[가-힣]*');

--시작 조건 검색은 like가 제일 빠르다 나머지는 instr이 우수하다.

/*

날짜조건

=문자열처럼 비교가 가능하지만 잘 사용하지 않는다.

=날짜는 system.currentTimemillis() 형태처럼 숫자로도 취급되어 계산이 가능하다.

=extract() 명령을 이용하면 원하는 항목만 추출할 수도있다.

*/

--2019년에 제조된 상품 정보를 조회

--1=2019년 1월 1일 0시 0분 0초부터 2019년 12월 31일 23시 59분 59초까지

--2=연도가2019인경우

--3=날짜가 2019로 시작하는경우

--1

--select * from product where made>= 2019년1월1일 0시 0분0초 and made<= 2019년 12월 31일 23시 59분 59초;

select * from product

where made>= to_date ('20190101000000','YYYYMMDDHH24MISS')

and made<= to_date ('20191231235959','YYYYMMDDHH24MISS') ;

select * from product

where made between to_date ('20190101000000','YYYYMMDDHH24MISS') and to_date ('20191231235959','YYYYMMDDHH24MISS') ;

--2

select * from product where extract(year from made)=2019;

--3

select * from product where made like '19/%';

select * from product where instr(made ,'19/')=1;

--1. 유통기한이 2021년인 제품을 출력

select * from product where extract(year from expire)=2021;

--2. 여름(6,7,8)월에 생산된 과자 목록을 출력

select * from product where type='과자' and extract(month from made) >= 6 and extract (month from made) <=8;

--3. 과자와 사탕 중에서 유통기한이 1년 이하인 제품의 목록을 출력

select * from product where type='과자'or type='사탕' and expire - made <=365;

--4. 최근 2년간 생상된 제품 목록을 출력 ( 730일 전부터 지금까지 )

select * from product where sysdate-made <=730;

commit;

'[DBMS] > - Oracle' 카테고리의 다른 글

[Oracle]서브쿼리(subQuery)  (0) 2022.05.30
[Oracle]듀얼테이블(dualTable)이란 ?  (0) 2022.05.30
[Oracle]날짜데이터 다루기  (0) 2022.05.29
[Oracle]제약조건  (0) 2022.05.29
[Oracle]테이블관리  (0) 2022.05.29

댓글