DB(Database)/MySQL

[MySQL] λ‚΄μž₯ν•¨μˆ˜ - μˆ˜ν•™ ν•¨μˆ˜, λ‚ μ§œ/μ‹œκ°„ ν•¨μˆ˜, μ‹œμŠ€ν…œ/정보 ν•¨μˆ˜

탱저 2021. 2. 6. 15:44

μˆ˜ν•™ ν•¨μˆ˜

  • ABS(숫자) : μ ˆλŒ€κ°’ 계산
  • CEILING(숫자) : 올림
  • FLOOR(숫자) : λ‚΄λ¦Ό
  • ROUNG(숫자) : 반올림
  • CONV(숫자, κΈ°μ‘΄ μ§„μˆ˜, λ°”κΏ€ μ§„μˆ˜) : κΈ°μ‘΄ μ§„μˆ˜μ—μ„œ λ‹€λ₯Έ μ§„μˆ˜λ‘œ λ°”κΏ”μ£ΌλŠ” ν•¨μˆ˜
SELECT ABS(-100);

SELECT CEILING(4.7), FLOOR(4.7), ROUND(4.7);

SELECT CONV('AA',16,2), CONV(100,10,8); 
-- 16μ§„μˆ˜ AAλ₯Ό 2μ§„μˆ˜λ‘œ λ³€κ²½, 10μ§„μˆ˜μ˜ 100을 8μ§„μˆ˜λ‘œ λ³€κ²½


  • MOD(숫자1, 숫자2), 숫자1 % 숫자2 : 숫자1을 숫자2둜 λ‚˜λˆˆ λ‚˜λ¨Έμ§€ λ°˜ν™˜
  • POW(숫자1, 숫자2) : 숫자1을 숫자2만큼 κ±°λ“­μ œκ³±ν•œ κ°’ λ°˜ν™˜
  • SQRT(숫자) : 숫자의 제곱근 λ°˜ν™˜ 
select mod(228, 10), 228%10, 228 mod 10;

select pow(3, 3), sqrt(25);


  • RAND() : 0이상 1미만의 μ‹€μˆ˜ λžœλ€κ°’ 좜λ ₯
  • SIGN(숫자) : μˆ«μžκ°€ μ–‘μˆ˜, 0, μŒμˆ˜μΈμ§€ ꡬ해 1, 0, -1 쀑 ν•˜λ‚˜ λ°˜ν™˜
  • TRUNCATE(숫자, μ •μˆ˜): μ†Œμˆ˜μ  κΈ°μ€€μœΌλ‘œ μ •μˆ˜ μœ„μΉ˜κΉŒμ§€ κ΅¬ν•˜κ³  λ‚˜λ¨Έμ§€ 버림
select rand(), floor(1 + (rand() * 6));
-- λžœλ€κ°’ 좜λ ₯, 였λ₯Έμͺ½ 쿼리문은 μ£Όμ‚¬μœ„ 숫자 λžœλ€ν•˜κ²Œ 좜λ ₯ν•΄μ£ΌλŠ” 쿼리문

select sign(123),sign(0),sign(-123);

select truncate(2222.2272,2), truncate(2222.2272,-2);


λ‚ μ§œ/μ‹œκ°„ ν•¨μˆ˜

  • ADDDATE(λ‚ μ§œ, 차이), SUBDATE(λ‚ μ§œ, 차이): λ‚ μ§œλ₯Ό κΈ°μ€€μœΌλ‘œ 차이λ₯Ό λ”ν•˜κ±°λ‚˜ λΊ€ λ‚ μ§œ λ°˜ν™˜
  • ADDTIME(λ‚ μ§œ/μ‹œκ°„, μ‹œκ°„), SUBTIME(λ‚ μ§œ/μ‹œκ°„, μ‹œκ°„): λ‚ μ§œ/μ‹œκ°„μ„ κΈ°μ€€μœΌλ‘œ μ‹œκ°„μ„ λ”ν•˜κ±°λ‚˜ λΊ€ κ²°κ³Ό λ°˜ν™˜
select adddate('2020-02-01', interval 15 day);
select adddate('2020-02-01', interval 15 month);
select adddate('2020-02-01', interval 15 year);
-- 주어진 λ‚ μ§œλ‘œλΆ€ν„° κ·Έ ν›„μ˜ λ‚ μ§œ 리턴

select subdate('2020-02-01', interval 15 day);
select subdate('2020-02-01', interval 15 month);
select subdate('2020-02-01', interval 15 year);
-- 주어진 λ‚ μ§œλ‘œλΆ€ν„° κ·Έ μ „μ˜ λ‚ μ§œ 리턴

select addtime('10:10:10', '1:1:1');
-- μ‹œκ°„ λ”ν•˜κΈ°

select subtime('10:10:10', '1:1:1');
-- μ‹œκ°„ λΉΌκΈ°


  • CURDATE(), CURTIME(), NOW(), SYSDATE()
    • CURDATE(), CURTIME(): ν˜„μž¬ μ—°-μ›”-일 λ°˜ν™˜, ν˜„μž¬ μ‹œ:λΆ„:초 λ°˜ν™˜
    • NOW(), SYSDATE(): ν˜„μž¬ μ—°-μ›”-일 μ‹œ:λΆ„:초 λ°˜ν™˜
  • YEAR(λ‚ μ§œ), MONTH(λ‚ μ§œ), DAY(λ‚ μ§œ), HOUR(μ‹œκ°„), MINUTE(μ‹œκ°„), SECOND(μ‹œκ°„), MICROSECOND(μ‹œκ°„)
    • λ‚ μ§œ λ˜λŠ” μ‹œκ°„μ—μ„œ μ—°, μ›”, 일, μ‹œ, λΆ„ 초, λ°€λ¦¬μ΄ˆ ꡬ함
  • DATE(), TIME() : DATETIME ν˜•μ‹μ—μ„œ μ—°-μ›”-일과 μ‹œ:λΆ„:초 좜λ ₯
  • DATEDIFF(λ‚ μ§œ1, λ‚ μ§œ2), TIMEDIFF(λ‚ μ§œ1 λ˜λŠ” μ‹œκ°„1, λ‚ μ§œ1 λ˜λŠ” μ‹œκ°„2) : λ‚ μ§œ1-λ‚ μ§œ2 κ²°κ³Ό λ°˜ν™˜
select curdate(), curtime(), now(), sysdate();

select year(curdate()), month(curdate()), day(curdate());
-- year()λŠ” λ‚ μ§œ, month()ν•¨μˆ˜λŠ” μ›”, day()λŠ” 일을 μΆ”μΆœν•΄μ„œ 좜λ ₯

select hour(curtime()), minute(curtime()), second(curtime());

select datediff('20200101', sysdate());
select datediff(now(), '20200101');
-- ν˜„μž¬λΆ€ν„° μΈμžκ°’κΉŒμ§€μ˜ λ‚ μ§œ 차이 리턴

select timediff('10:10:10', curtime());
select timediff(curtime(), '10:10:10');
-- μ‹œκ°„ 차이 리턴


  • DAYOFWEEK(λ‚ μ§œ), MONTHNAME(), DAYOFYEAR(λ‚ μ§œ)
    • DAYOFWEEK(): μš”μΌ(1~7: μΌμš”μΌλΆ€ν„° ν† μš”μΌ) λ°˜ν™˜
    • MONTHNAME(): μ›”μ˜ μ˜λ¬Έμ΄λ¦„ λ°˜ν™˜
    • DAYOFYEAR(): 1λ…„ 쀑 λͺ‡ 번째 날인지(1~366) λ°˜ν™˜
  • LAST_DAY(λ‚ μ§œ): μž…λ ₯ν•œ μ›”μ˜ λ§ˆμ§€λ§‰ λ‚ μ§œ λ°˜ν™˜
  • MAKEDATE(연도, μ •μˆ˜): μ—°λ„μ˜ 첫날뢀터 μ •μˆ˜λ§ŒνΌ μ§€λ‚œ λ‚ μ§œ λ°˜ν™˜
  • MAKETIME(μ‹œ, λΆ„, 초): μ‹œ, λΆ„, 초 μ΄μš©ν•΄ μ‹œ:λΆ„:초 TIME ν˜•μ‹ λ§Œλ“€μ–΄μ€Œ
  • PERIOD_ADD(μ—°μ›”, κ°œμ›”μˆ˜): μ—°μ›”λΆ€ν„° κ°œμ›”μˆ˜λ§ŒνΌ μ§€λ‚œ μ—°μ›” λ°˜ν™˜
  • PERIOD_DIFF(μ—°μ›”1, μ—°μ›”2): μ—°μ›”1-μ—°μ›”2 κ°œμ›” 수 λ°˜ν™˜
  • QUARTER(λ‚ μ§œ): λ‚ μ§œκ°€ 4λΆ„κΈ° μ€‘μ—μ„œ μ–΄λŠ 뢄기에 ν•΄λ‹Ήν•˜λŠ”μ§€ λ°˜ν™˜
  • TIME_TO_SEC(μ‹œκ°„): μ‹œκ°„μ„ 초 λ‹¨μœ„λ‘œ λ°˜ν™˜
select dayofweek(curdate()), monthname(curdate()), dayofyear(curdate());
-- dayofweek()μš”μΌμ„ μƒμˆ˜κ°’μœΌλ‘œ λ¦¬ν„΄ν•œλ‹€. μΌμš”μΌ 1, μ›”μš”μΌ 2...
-- monthname()ν•¨μˆ˜λŠ” λ‹¬μ˜ 이름을 μ˜μ–΄λ‘œ 좜λ ₯
-- dayofyear()ν•¨μˆ˜λŠ” 1λ…„ 쀑에 λͺ‡μΌ 째인지 리턴

select last_day(curdate());
select last_day('20200101');
-- last_day() 주어진 λ‹¬μ˜ λ§ˆμ§€λ§‰λ‚  좜λ ₯

select makedate(2020, 60);
-- makedate()ν•¨μˆ˜λŠ” 2020λ…„μ—μ„œ 60일째 λ˜λŠ” 날을 리턴

select maketime(12,10,10);
-- maketime() μΈμžκ°’μœΌλ‘œ μ‹œκ°„μ„ λ§Œλ“€μ–΄ 리턴

select period_add('202001', 11), period_diff(202001, 201812);

select concat(quarter(curdate()), '사뢄기') as 'λΆ„κΈ°';
-- quarter() λͺ‡μ‚¬λΆ„기인지 리턴

select time_to_sec('12:12:12');
-- μ‹œκ°„μ„ 초둜 ν™˜μ‚°


λ‚΄μž₯ ν•¨μˆ˜(μ‹œμŠ€ν…œ/정보 ν•¨μˆ˜)

  • USER(), DATABASE(): ν˜„μž¬ μ‚¬μš©μžμ™€ ν˜„μž¬ μ„ νƒλœ λ°μ΄ν„°λ² μ΄μŠ€ λ°˜ν™˜
  • FOUND_ROWS(): λ°”λ‘œ μ•ž μΏΌλ¦¬λ¬Έμ—μ„œ 쑰회된 ν–‰μ˜ 개수 λ°˜ν™˜
  • ROW_COUNT(): λ°”λ‘œ μ•žμ˜ INSERT, DELETE, UPDATE λ¬Έμ—μ„œ μ‚½μž…, μ‚­μ œ, μˆ˜μ •λœ ν–‰μ˜ 개수 λ°˜ν™˜
  • SLEEP(초): 쿼리 μ‹€ν–‰ 잠깐 멈좀
  • VERSION(): ν˜„μž¬ MySQL 버전 좜λ ₯
use sqldb;
select current_user(), database();

select *
  from buytbl;
  
select found_rows();
-- found_rows()λŠ” 이전 쑰회된 rows의 건수λ₯Ό λ¦¬ν„΄ν•œλ‹€.

update buytbl
   set price = price/2;

select row_count();
-- row_count() 이전 update,delete,insertν•œ 건수λ₯Ό λ¦¬ν„΄ν•œλ‹€.(λ²„μ „λ³„λ‘œ 상이함)

select sleep(5);
select '5μ΄ˆν›„μ— 이게 λ³΄μž…λ‹ˆλ‹€';
select version();
-- sleep()λŠ” 주어진 μΈμžκ°’λ§ŒνΌ λ©ˆμΆ”μ–΄μ€€λ‹€.
-- version 좜λ ₯

 

728x90