2. Mysql 함수(2)      -바닥부터 배우는 Mysql -

 

'+' , '-' , '*' , '/', '=', '<=', '>=', '!=' 또는 '<>', '<', '>', '<=>'

BETWEEN minValue AND maxValue,

IN()

ASCII(), CHAR(), FIELD()

LOWER()=LCASE(), UPPER()=UCASE()

LOAD_FILE()

REVERSE()

INSERT()

SUBSTRING_INDEX()

REPEAT()

LTRIM(), RTRIM(),TRIM()

CONCAT(), CONCAT_WS(), LENGTH()

LEFT(), RIGHT(), SUBSTRING(), LOCATE()

LPAD(), RPAD()

SPACE(), REPLACE()

 

1. 산술 연산자:

'+' , '-' , '*' , '/' 등을 사용할 수 있다.

2-1

SELECT

(20+5-2)/3,

(32+4)/6,

5*4/10,

4/0;

결과

(20+5-2)/3

(32+4)/6

5*4/10

4/0

7.6667

6.0000

2.0000

 

4/0의 경우는 null을 출력한다.

 

2. 비교 연산자:

비교 연산자의 몇 가지 규칙

인수가 모두 문자열이면 문자열로 비교.

인수가 모두 정수면 정수로 비교.

인수가 모두 "null" 이면 연산의 결과도 "null"이 됨.

한쪽의 인수가 'timestamp' 이면 나머지도 같이 변환되어 비교됨.

한쪽의 인수가 "datetime" 이면 나머지도 같이 변환되어 비교됨.

나머지의 경우는 부동소수점 실수로 비교됨.

 

=, <=, >=, != 또는 <>, <, >, <=>(둘 중 하나가 'null'이면 0을 출력)

2-2

SELECT 'apple' <>'banana',

100 >= 20,

2.8 = 3.8;

결과

'apple' <>'banana'

100 >= 20

2.8 = 3.8

1

1

0

 

SELECT NULL <=> NULL,

100 <=> NULL;

결과

NULL <=> NULL

100 <=> NULL

1

0

 

인자 between (최소 값) and (최대 값) : 인자가 최소 값과 최대 값 사이에 존재하면 1())을 그렇지 않을 경우 0(거짓)을 출력.

인자 in (1, 2, ) : 인자가 ( ) 안에 존재하면 1()을 그렇지 않으면 0(거짓)을 출력.

2-3

SELECT 7 BETWEEN 5 AND 9,

'a' IN ('a', 'b', 'c');

결과

7 BETWEEN 5 AND 9

'a' IN ('a', 'b', 'c')

1

1

 

 

3. 문자열 관련 함수:

ASCII(str) : str의 처음 위치의 ASCII 코드를 리턴. (NULLNULL로 리턴.)

CHAR(n,...) : n(1-256의 정수 값)의 아스키코드를 해당 문자로 출력한다.

FIELD(Nstring , string1, string2,) : Nstring과 같은 문자열이 몇 번째인지를 리턴.

2-4

SELECT ascii('abc') as ascII,

char(77,121,83,81,'76') as char_,

field('red', 'black','blue','red') as field_;

결과

ascII

char_

field_

97

MySQL

3

 

LOWER(str) : str을 소문자로 변환. ( =LCASE(str) )

UPPER(str) : str을 대문자로 변환. ( =UCASE(str) )

LOAD_FILE(파일명) : 64kb 보다 작은 내용의 파일을 읽어 들여 문자열로 리턴.

(64kb보다 크면 NULL을 리턴.)

2-5

SELECT lower('KOREA'),

upper('usa');

lower('KOREA')

upper('usa')

korea

USA

 

REVERSE(str) : str의 순서를 바꾸어 리턴.

2-6

SELECT reverse('Some Text') as reverse;

reverse

txeT emoS

 

INSERT(str0, a, b, str1) : str0a 위치부터 b 만큼의 문자를 str1으로 대치시킨다.

2-6

SELECT insert('korea',1,0,'***') as case1,

insert('korea',2,0,'***') as case2,

insert('korea',3,0,'***') as case3,

insert('korea',3,1,'***') as case4,

insert('korea',3,2,'***') as case5,

insert('korea',3,3,'***') as case6;

결과

case1

case2

case3

case4

case5

case6

***korea

k***orea

ko***rea

ko***ea

ko***a

ko***

 

SUBSTRING_INDEX(str,delim,count) : str을 구분자(delimer)로 구분해서 배열로 만들고 count 만큼 리턴. 오른쪽부터 출력하길 원하면 음수를 적는다.

2-7

SELECT SUBSTRING_INDEX('a.b.c', '.', 3) as substr_inx1,

SUBSTRING_INDEX('a.b.c', '.', 2) as substr_inx2,

SUBSTRING_INDEX('a.b.c', '.', -2) as substr_inx3;

결과

substr_inx1

substr_inx2

substr_inx3

a.b.c

a.b

b.c

 

REPEAT(str,count) : strcount 만큼 반복.

SELECT REPEAT('Triple',3) as repeat_3;

2-8

repeat_3

TripleTripleTriple

 

LTRIM(str) : str의 왼쪽 공백을 제거.

RTRIM(str) : str의 오른쪽 공백을 제거.

TRIM(str) : str의 양쪽 공백을 제거.

2-9

SELECT ltrim(' Seoul') as ltrim,

rtrim('Seoul ') as rtrim,

trim(' Seoul ') as trim;

결과

ltrim

rtrim

trim

Seoul

Seoul

Seoul

 

CONCAT(str1, str2,) : 해당 str을 이어 줍니다.

CONCAT_WS(separator, str1, str2,...) : str1, str2 등을 구분자를 사용하여 하나의 문장으로 출력.

LENGTH(str) : str의 길이를 출력.

2-10

SELECT concat('Mon','s','ter ','inc.') as concat,

concat_ws(':', 'red', 'blue', 'cyan') as concat_ws,

length('count this!')as length;

결과

concat

concat_ws

length

Monster inc.

red:blue:cyan

11


LEFT(str, n) : str의 왼쪽부터 n 개를 리턴.

RIGHT(str, n) : str의 오른쪽부터 n 개를 리턴.

SUBSTRING(str,pos,n) : 문자열 str의 특정 시작위치 pos에서 n개의 문자를 출력.(=MID(str,pos,n))

LOCATE(substr,str) : str에서 substr이 위치한 첫 번째 위치를 정수 값으로 출력.

2-11

SELECT left('Study_zone',4) as left_,

right('Study_zone',2) as right_,

substring('Study_zone',1,5) as substring,

locate('of', 'The beginning is half of the whole.') as locate;

결과

left_

right_

substring

locate

Stud

ne

Study

23

 

LPAD(str, n, padstr) : str에 왼쪽부터 padstr 문자를 넣어 전체 길이를 n 만큼 만든다.

RPAD(str, n, padstr) : str에 오른쪽부터 padstr 문자를 넣어 전체 길이를 n 만큼 만든다.

2-12

SELECT lpad('Sql',3,'My') as example1,

lpad('Sql',4,'My') as example2,

lpad('Sql',5,'My') as example3,

lpad('Sql',6,'My') as example4,

rpad('Sql',2,'wow') as example5,

rpad('Sql',5,'wow') as example6,

rpad('Sql',7,'wow') as example7;

결과

example1

example2

example3

example4

example5

example6

example7

Sql

MSql

MySql

MyMSql

Sq

Sqlwo

Sqlwoww

 

SPACE(n) : n 만큼 공백을 리턴.

REPLACE(str,from_str,to_str) : 문자열 str에서 특정문자 from_str을 특정 문자 to_str로 바꾸어 출력한다.

2-13

SELECT REPLACE('www.mysql.com', 'w', 'Ww');

결과

REPLACE('www.mysql.com', 'w', 'Ww')

WwWwWw.mysql.com

 

Posted by star story :

Snippet :: Code View :: "+location.href+'
'+b+"

");top.consoleRef.document.close()}$(".snippet-container").each(function(b){$(this).find("a.snippet-text").click(function(){var d=$(this).parents(".snippet-wrap").find(".snippet-formatted");var c=$(this).parents(".snippet-wrap").find(".snippet-textonly");d.toggle();c.toggle();if(c.is(":visible")){$(this).html("html")}else{$(this).html("text")}return false});$(this).find("a.snippet-window").click(function(){var c=$(this).parents(".snippet-wrap").find(".snippet-textonly").html();a(c);$(this).blur();return false})});$(".snippet-toggle").each(function(b){$(this).click(function(){$(this).parents(".snippet-container").find(".snippet-wrap").toggle()})})});