MySQL Exists(존재여부), Millisecond

MySQL에서의 존재여부 확인은 MSSQL과 조급 다르네요. 주로 .net위주의 저로서는 (Mssql을 많이 사용했습니다.) 멘붕 입니다.ㅎ


하여 오늘은 MySQL Exists(존재여부), Millisecond에 대해서 알아보겠습니다.
존재여부 명령어인 Exists를 알아보면서 MSSQL과 조금 다른  Millisecond에 대해서도 같이 알아보겠습니다.




MSSQL에서는 존재여부를 아래와 같이 사용합니다. (특정 테이블에 해당 값이 있으면 update, 없으면 insert)

IF EXISTS (SELECT 1 FROM 테이블명 WHERE 칼럼명1 = 값1)
BEGIN
    UPDATE 테이블명 SET 칼럼명2 = 값2 WHERE 칼럼명1 = 값1;
END
ELSE
BEGIN
    INSERT INTO 테이블명 (칼럼명~~~) VALUES (값~~~);
END

----------------------------------------------------------------

그런데 MySQL은 아래와 같이 조금 다르게 사용합니다.

IF (SELECT 1 = 1 FROM 테이블명 WHERE 칼럼명1 = 값1) THEN
BEGIN
    UPDATE 테이블명 SET 칼럼명2 = 값2 WHERE 칼럼명1 = 값1;
END;
ELSE
BEGIN
    INSERT INTO 테이블명 (칼럼명~~~) VALUES (값~~~);
END;
END IF;

** 참고로 MySQL의 if문 사용에서 위와 같이 사용 가능한 것은 프로시저와 함수에서 사용가능합니다. 일반 쿼리창의 쿼리문에서는 SELECT IF (1 = 1, '참', '거짓'); 이렇게 사용하셔야 합니다.




다음으로 Millisecond의 사용인데요.
현재 날짜의 밀리세컨드를 사용해야 하는 일이 있어 알고있는 지식으로 new()을 해보니 안됩니다. 하여 찾아본 결과 밀리세컨드를 사용하려면 now(6)를 이용해야 한다고 합니다.ㅎ
아래는 select과 같이 사용하는 예 입니다.

select date_format(now(6), '%Y%m%d%H%i%s%f')

** 참고로 DB의 특정 테이블의 특정 칼럼에 날짜 타입으로 밀리세컨드까지 입력을 시키려면 데이타타입(datatype)가 datetime(6)으로 설정되 있어야 합니다.

밑에는 MySql 공식 홈페이지에서 발췌한 시간관련 Specifier 입니다.


SpecifierDescription
%aAbbreviated weekday name (Sun..Sat)
%bAbbreviated month name (Jan..Dec)
%cMonth, numeric (0..12)
%DDay of the month with English suffix (0th, 1st, 2nd, 3rd, …)
%dDay of the month, numeric (00..31)
%eDay of the month, numeric (0..31)
%fMicroseconds (000000..999999)
%HHour (00..23)
%hHour (01..12)
%IHour (01..12)
%iMinutes, numeric (00..59)
%jDay of year (001..366)
%kHour (0..23)
%lHour (1..12)
%MMonth name (January..December)
%mMonth, numeric (00..12)
%pAM or PM
%rTime, 12-hour (hh:mm:ss followed by AM or PM)
%SSeconds (00..59)
%sSeconds (00..59)
%TTime, 24-hour (hh:mm:ss)
%UWeek (00..53), where Sunday is the first day of the week; WEEK() mode 0
%uWeek (00..53), where Monday is the first day of the week; WEEK() mode 1
%VWeek (01..53), where Sunday is the first day of the week; WEEK() mode 2; used with %X
%vWeek (01..53), where Monday is the first day of the week; WEEK() mode 3; used with %x
%WWeekday name (Sunday..Saturday)
%wDay of the week (0=Sunday..6=Saturday)
%XYear for the week where Sunday is the first day of the week, numeric, four digits; used with %V
%xYear for the week, where Monday is the first day of the week, numeric, four digits; used with %v
%YYear, numeric, four digits
%yYear, numeric (two digits)
%%A literal % character
%xx, for any x not listed above





이상으로 MySQL Exists(존재여부), Millisecond에 대해서 알아보았습니다.
끝까지 읽어주셔서 감사합니다.

댓글

이 블로그의 인기 게시물

껌 떼는 법 (완벽 제거)

학점 계산기 (백분위 환산) 완벽 정리

[엑셀] 셀에 수식 적용하는 모든 것 완벽 정리