주석처리
— 한줄 주석 연습
여러줄 주석
/* 내용 */
select select_expr
[from table_references]
[where where_condition]
[group by [col_name | expr | positoin}]
[having wherer_condition]
[order by {col_name | expr | postion}]
limit
order by, group by , having
order by : 정렬순서 지정하기
group by : 원하는 아이디를 그룹을 만들어서 정렬하기 (ID값을 그룹 지어서 순서 정렬)
use sqldb; select * from buytbl; -- 아이디를 순서데로 정렬 -- select userID, amount from buytbl order by userID; -- 아이디를 그룹지어서 아이디와 구매합계를 정렬 -- select userID, sum(amount) from buytbl group by userID; -- 알리아스표현 -- select userID AS '사용자 아이디' , sum(amount) AS '총구매 갯수' from buytbl group by userID; select userID AS '사용자 아이디' , sum(price * amount) AS '총구매 금액' from buytbl group by userID;
합계를 비롯한 다양한 수식함수
sum, avg, min, max, count, count(distinct). sedev, var_samp
select userID AS '사용자 아이디' , sum(amount) AS '총구매 갯수' from buytbl group by userID; select userID AS '사용자 아이디' , sum(price * amount) AS '총구매 금액' from buytbl group by userID; select userID AS '사용자 아이디' , avg(price * amount) AS '평균금액' from buytbl group by userID; select userID AS '사용자 아이디' , min(price * amount) AS '최소금액' from buytbl group by userID; select userID AS '사용자 아이디' , count(price * amount) AS '구매횟수' from buytbl group by userID; select userID AS '사용자 아이디' , stddev(price * amount) AS '표준편차' from buytbl group by userID; select userID AS '사용자 아이디' , var_samp(price * amount) AS '분산' from buytbl group by userID;
구매자가 구매한 물품의 개수를 평균를 구하는 식
select avg(amount) as '평균 구매 개수' from buytbl; select userID , avg(amount) as '평균 구매 개수' from buytbl group by userID;
subQeury 조합하기
select name, height from usertbl; select name, max(height), min(height) from usertbl; -- 틀림 -- select name, max(height), min(height) from usertbl group by Name; -- 틀림 -- -- 한명씩 서브쿼리를 생성해서 최대키, 최소키의 값을 뽑아낸다. select name, height from usertbl where height = (select max(height) from usertbl ) or height = ( select min(height) from usertbl); (가장 큰 키값을 한명 뽑아내고) or (가장 작은 키 값을 뽑아내고) 표현은 서브쿼리로 돌려야 한다. 휴대폰이 있는 사용자 수 select count(mobile1) AS '휴대폰이 있는 사용자' from usertbl; 구매테이블에서 구매를 가장 많이 회원을 불러오는 쿼리 오름차순과 내림차순을 이용해서 가장 상위값만 불러오기 select userID as '사용자' , sum(price*amount) as '총구매액' from buytbl; select userID as '사용자' , sum(price*amount) as '총구매액' from buytbl group by userID; select userID as '사용자' , sum(price*amount) as '총구매액' from buytbl group by userID order by userID; select userID as '사용자' , sum(price*amount) as '총구매액' from buytbl group by userID order by userID desc limit 1; select userID as '사용자' , sum(price*amount) as '총구매액' from buytbl group by userID order by userID asc limit 1;
any/all/some , subQeury
-- 177보다 키가 큰 사람 선택하기 -- select name, height from usertbl where height > 177; -- 김경호의 키를 불러와서 그 키값보다 큰 사람 선택하기 , 김경호 키가 177이므로 결국 같은 데이터 - select name, height from usertbl where height > (select height from usertbl where name = '김경호'); -- 오류 원인을 파악하기 , 경남의 키값은 2개가 반환이 된다. 정확한 기준이 없기 때문에 오류가 난다. -- select name, height from usertbl where height > (select height from usertbl where addr = '경남'); -- any를 붙이면 173, 170 모두 해당이 되어 결과적으로 170 이상을 모두 선택하게 된다. -- select name, height from usertbl where height >= any(select height from usertbl where addr = '경남'); -- all을 붙이면 173,170 모두 만족이 되어야 하기 때문에 173 이상을 모두 선택하게 된다. -- select name, height from usertbl where height >= all(select height from usertbl where addr = '경남'); -- some, any 동일한 의미 -- select name, height from usertbl wherer height = ANY( select height from usertbl where addr = '경남'); select name, height from usertbl where height >= any(select height from usertbl where addr = '서울'); -- in 사용법 select name, height from usertbl where height IN ( select height from usertbl where addr = '경남');
select name, height from usertbl where height in (select height from usertbl where addr = '경남');
order by 오름차순, 내림차순 살펴보기
select name, mDate from usertbl order by mDate; select name, mDate from usertbl order by mDate DESC; select name, mDate from usertbl order by mDate ASC; ASC는 기본값 select name, name from usertbl order by height DESC, name ASC; 2개를 처리
order by는 mysql 성능을 떨어뜨릴 소지가 있어서 필요할 때만 사용한다.
distinct , limit
select addr from usertbl order by addr; select distinct addr from usertbl order by addr;
select distinct addr, height from usertbl order by height asc limit 3; select distinct addr, height from usertbl order by height desc limit 3; select distinct addr, height from usertbl order by height desc limit 0, 3;
중복출력되는것을 하나로 대치하는 명령, 쿼리 결과가 많을 때 제한 걸기
order by, asc , limit 로 일부만 출력하기