email
을 delete
한다.email
이 있을 때 id
가 작은 것을 남긴다.delete from Person where id not in (
select id from (
select min(id) as id
from Person
group by email
) as tmp
);
not in
조건에는 삭제되지 않는 id
가 조회되도록 한다.
중복을 제거하고자 하는 칼럼(email)을 기준으로 group by
를 하게 되면 일단 중복은 제거된다.
group by
로 중복 제거 후 id
기준 작은 값을 살리기 위해 min(id)
를 사용한다.
select w1.id
from Weather w1, Weather w2
where w1.temperature > w2.temperature
and datediff(w1.recordDate, w2.recordDate) = 1;
datediff
datefidd(날짜1, 날짜2);
[ 날짜1 - 날짜2 ]
description
이 boring
이 아니고 id
가 홀수인 row를 rating
기준 내림차순 정렬하여 조회하라.select id, movie, description, rating
from Cinema
where description != 'boring' and mod(id,2)!=0
order by rating desc;
나머지 연산
mod(칼럼명, 나눌값)
id
가 홀수번째인 경우 id
에 +1id
가 짝수번째인 경우 id
에 -1id
가 홀수번째이고 마지막 id
인 경우 현재 id
그대로 출력위 세가지 조건을 case-when-end
문으로 표현한다.
select (
case
when mod(id, 2) = 1 and id = (select count(*) from Seat) then id
when mod(id, 2) = 1 then id+1
else id-1
end
) as id, student
from Seat
order by id;
조건에 의해 변경한 id
대로 조회된 결과가 정렬되도록 order by
사용
case
when 조건1 값
when 조건2 값
else 값
end as 뱔칭;
sex
가 m
인 경우 f
로 f
인 경우 m
으로 변경하라update Salary
set sex = (
case sex
when 'f' then 'm'
else 'f'
end
);
UPDATE 테이블명
SET 칼럼1 = 값1, 칼럼2 = 값2;