0628 숙제
(1) 데이터가져오기
select * from table명;
ex) select * from personal;
select pno, pname, pay from personal;
select * from persolnal where dno=10;
(2) 비교연산
ex) select * from personal where dno>20;
(3) 논리연산(다중조건) and(&&) or(||)
ex) select * from personal where dno=20 and pno>1111;
(4) in
ex) select * from personal where dno in (10,20);
where dno=10 or dno=20;
where dno=10 || dno=20;
(5) between
ex) select * from personal where dno between 20 and 30;
(6) 부분검색
ex) select * from personal where job like 's%'; #s로 시작하는 단어
where job like '%r'; #r로 끝나는 단어
where dno like '3_';
(7) 부정
not like
not between
테이블의 검색 결과 출력 관련
(1) order by
ex) select * from personal order by bonus asc; #bonus를 기준으로 오름차순 정렬
select * from personal order by dno desc; #dno를 기준으로 내림차순 정렬
(2) distinct(중복제거)
ex) select dno from personal; #중복 포함 모든 데이터를 가져옴.
select distinct dno from personal; #중복된 데이터는 생략하고 가져옴.
(3) As
ex) select pname, pay, pay+bonus as income from personal;
select pname, pay, pay+bonus as 'month incom' from personal order by 'month incom';
select pname, pay, startdate from personal order by 2;
(4) 표현식을 이용한 값
ex) select pname, pay, pay+bonus from personal;
참고) null+값 = null
ex) select pname, pay, pay + ifnull(bonus,0) from personal;
(5) 컬럼함수
sum, avg, min, max, count
참고) null데이터는 제외됨.
ex) select sum(pay), avg(pay), min(pay), max(pay), count(*) from personal;
참고) count(*)은 where조건을 충족하는 테이블의 건수.
ex) select count(distinct dno), count(*) from personal;
(6) group by 필드명
having 그룹조건
order by 필드명 (asc 또는 desc)
ex) select dno, sum(pay) from personal;
select dno, sum(pay) from personal group by dno;
select dno, sum(pay) from personal where pay>1500 group by dno;
select dno, sum(pay) from personal where pay >1500 group by dno having sum(pay) >6000;
(7) scalar 함수
단일값 추출
[1] select substr(job,1,4) from personal;
[2] 문자열 결합하기
ex) select concat(pname,' job is ', job) from personal;
비교) select pno, ' job is ', job from personal;
[3] round(3.141592,3) , truncate(3.141592,3)
round(3.141592,-3) , truncate(3.141592,-3)
[4] 치환 replace
select replace(job, 'manager', 'man') from personal;
[5] 날짜 함수
(a) 요일
select dayname(startdate) from personal;
(b) 날짜
select to_days(now()) - to_days(startdate) from personal;
(8) null값 추출
ex) select pname from personal where bonus is null;
(9) Limit
ex) select pname from personal limit 5;
(연습문제)
1. 업무가 clerk가 아니고 manager이 아닌 사원.
2. 입사일이 1991이후 사원중 급여가 2000 이하인 사원.
select * from personal where startdate>='1991-01-01' and pay>=2000;
3. 업무가 president 거나 salesman인 사원.
4. 사원이름 첫 글자가 a부터 k까지 인 사원을 이름순으로 정렬.
select * from personal where substr(pname,1,1)>='a' && substr(pname,1,1)<='k';
(10) 논리함수
[1] if(논리식, 논리식이 참일 때 값, 논리식이 거짓일 때 값)
select pno, pname, pay, if(pay>=1500, 'good','poor') as result from personal;
[2] ifnull(값1, 값2)
select pno, pname, pay, ifnull(bonus, 100) from personal;