SELECT Set


SELECT로 Set 데이터를 조회

SELECT로 Set 데이터를 조회합니다.

테스트 데이터 입력

Example

명령>sadd myset1 mem10 mem20 mem30 mem40 mem50
결과>5
명령>sadd myset2 10 20 30 40 50
결과>5
명령>sadd myset3 45.6 12.3 34.5 23.4 56.7
결과>5

SELECT

SELECT *
SELECT key, value
SELECT key
SELECT value
SELECT key key_name or key AS key_name --> alias(별명) 사용 가능

FROM

FROM set.*
FROM set.myset*
FROM set.myset1

SELECT * FROM SET.MYSET*

Set의 키와 값(value)를 조회한다.

Example

명령>select * from set.myset*;
결과>   0) key|value
  1) myset1|mem30
  2) myset1|mem20
  3) myset1|mem40
  4) myset1|mem10
  5) myset1|mem50
  6) myset2|10
  7) myset2|20
  8) myset2|30
  9) myset2|40
10) myset2|50
11) myset3|23.4
12) myset3|34.5
13) myset3|12.3
14) myset3|45.6
15) myset3|56.7
명령>select * from set.myset1;
결과> 0) key|value
1) myset1|mem30
2) myset1|mem20
3) myset1|mem40
4) myset1|mem10
5) myset1|mem50
명령>select key from set.myset*;
결과> 0) key
1) myset1
2) myset2
3) myset3

ORDER BY

정렬(sort)해서 보여준다.

Example

명령>select value from set.myset1 order by value;
결과> 0) value
1) mem10
2) mem20
3) mem30
4) mem40
5) mem50
명령>select value from set.myset1 order by value desc;
결과> 0) value
1) mem50
2) mem40
3) mem30
4) mem20
5) mem10

정렬(sort) 순서: 숫자 < 문자

Example

명령>select * from set.myset* order by value;
결과>   0) key|value
  1) myset2|10
  2) myset3|12.3
  3) myset2|20
  4) myset3|23.4
  5) myset2|30
  6) myset3|34.5
  7) myset2|40
  8) myset3|45.6
  9) myset2|50
10) myset3|56.7
11) myset1|mem10
12) myset1|mem20
13) myset1|mem30
14) myset1|mem40
15) myset1|mem50

LIMIT

출력 행 수를 제한한다.

Example

명령>select * from set.myset* order by value limit 5;
결과> 0) key|value
1) myset2|10
2) myset3|12.3
3) myset2|20
4) myset3|23.4
5) myset2|30
명령>select * from set.myset* order by value desc limit 5;
결과> 0) key|value
1) myset1|mem50
2) myset1|mem40
3) myset1|mem30
4) myset1|mem20
5) myset1|mem10

LIMIT OFFSET

몇 행 건너 출력

Example

명령>select * from set.myset* order by value limit 5 offset 3; --> 3행 건너 5행 출력
결과> 0) key|value
1) myset3|23.4
2) myset2|30
3) myset3|34.5
4) myset2|40
5) myset3|45.6
명령>select * from set.myset* order by value limit 3, 5; --> 3행 건너 5행 출력
결과> 0) key|value
1) myset3|23.4
2) myset2|30
3) myset3|34.5
4) myset2|40
5) myset3|45.6

FUNCTIONS

COUNT()

개수를 조회한다.

Example

명령>select count(*) from set.myset*;
결과> 0) count(*)
1) 15
명령>select count(key) from set.myset*;
결과> 0) count(key)
1) 3
명령>select key, count(*) from set.myset* group by key;
결과> 0) key|count(*)
1) myset1|5
2) myset2|5
3) myset3|5

MAX(), MIN()

최댓값, 최솟값을 조회한다.

Example

명령>select min(value),max(value) from set.myset1;
결과> 0) min(value)|max(value)
1) mem10|mem50
명령>select min(value),max(value) from set.myset*;
결과> 0) min(value)|max(value)
1) 10|mem50
명령>select key,min(value),max(value) from set.myset* group by key;
결과> 0) key|min(value)|max(value)
1) myset1|mem10|mem50
2) myset2|10|50
3) myset3|12.3|56.7

MAX(value1, value2): 둘 중 큰 값 출력
MIN(value1, value2): 둘 중 작은 값 출력

Example

명령>select max('A','B')
결과> 0) max('A','B')
1) B
명령>select max(30,value) from set.myset2;
결과> 0) max(30,value)
1) 30
2) 30
3) 30
4) 40
5) 50
명령>select min('mem30',value) from set.myset1;
결과> 0) min('mem30',value)
1) mem30
2) mem20
3) mem30
4) mem10
5) mem30

숫자 함수

SUM(), AVG()

합계, 평균을 구한다.

Example

명령>select sum(value) from set.myset2;
결과> 0) sum(value)
1) 150
명령>select avg(value) from set.myset2;
결과> 0) avg(value)
1) 30.0

문자, 숫자가 섞여 있을 경우에는 숫자만 계산한다.

Example

명령>select sum(value) from set.myset*
결과> 0) sum(value)
1) 322.5

기타 함수

group_concat

Example

명령>select key,group_concat(value) from set.myset1;
결과> 0) key|group_concat(value)
1) myset1|mem10,mem40,mem30,mem20,mem50
명령>select key,group_concat(value,'') from set.myset1;
결과> 0) key|group_concat(value)
1) myset1|mem10mem40mem30mem20mem50
명령>select key,group_concat(value,' and ') from set.myset1;
결과> 0) key|group_concat(value)
1) myset1|mem10 and mem40 and mem30 and mem20 and mem50


WHERE 조건

비교: =, <, <=, >, >=, !=, <>

값(value) 비교: 숫자

Example

명령>select * from set.myset2 where value = 30;
결과> 0) key|value
1) myset2|30
명령>select * from set.myset2 where value > 30;
결과> 0) key|value
1) myset2|40
2) myset2|50
명령>select * from set.myset2 where value < 30;
결과> 0) key|value
1) myset2|10
2) myset2|20
명령>select * from set.myset2 where value != 30;
결과> 0) key|value
1) myset2|10
2) myset2|20
3) myset2|40
4) myset2|50

값(value) 비교: 문자

Example

명령>select * from set.myset1 where value > 'mem30';
결과> 0) key|value
1) myset1|mem40
2) myset1|mem50
명령>select * from set.myset1 where value >= 'mem30';
결과> 0) key|value
1) myset1|mem40
2) myset1|mem30
3) myset1|mem50
명령>select * from set.myset1 where value != 'mem30';
결과> 0) key|value
1) myset1|mem10
2) myset1|mem40
3) myset1|mem20
4) myset1|mem50

AND, OR

Example

명령>select * from set.myset2 where value >= 20 and value < 40;
결과> 0) key|value
1) myset2|20
2) myset2|30
명령>select * from set.myset2 where value <= 20 or value > 40;
결과> 0) key|value
1) myset2|10
2) myset2|20
3) myset2|50

BETWEEN

Example

명령>select * from set.myset2 where value between 20 and 40;
결과> 0) key|value
1) myset2|20
2) myset2|30
3) myset2|40
명령>select * from set.myset2 where value not between 20 and 40;
결과> 0) key|value
1) myset2|10
2) myset2|50

LIKE

%(퍼센트), _(밑줄)
대소문자를 구분하지 않는다.

Example

명령>sadd myset4 USER-A01 USER-A02 USER-B01 USER-B02 USER-C01
결과> 5
명령>select * from set.myset4 where value like 'user-a%';
결과> 0) key|value
1) myset4|USER-A02
2) myset4|USER-A01
명령>select * from set.myset4 where value like '%01%';
결과> 0) key|value
1) myset4|USER-A01
2) myset4|USER-B01
3) myset4|USER-C01

NOT LIKE

Example

명령>select * from set.myset4 where value not like 'user-a%';
결과> 0) key|value
1) myset4|USER-B02
2) myset4|USER-B01
3) myset4|USER-C01
명령>select * from set.myset4 where value not like '%01%';
결과> 0) key|value
1) myset4|USER-A02
2) myset4|USER-B02

GLOB

*(별표), ?(물음표)
대소문자를 구분한다.

Example

명령>select * from set.myset4 where value glob 'USER-A*';
결과> 0) key|value
1) myset4|USER-A02
2) myset4|USER-A01
명령>select * from set.myset4 where value glob '*01*';
결과> 0) key|value
1) myset4|USER-A01
2) myset4|USER-B01
3) myset4|USER-C01

NOT GLOB

Example

명령>select * from set.myset4 where value not glob 'USER-A*';
결과> 0) key|value
1) myset4|USER-B02
2) myset4|USER-B01
3) myset4|USER-C01
명령>select * from set.myset4 where value not glob '*01*';
결과> 0) key|value
1) myset4|USER-A02
2) myset4|USER-B02

IN

Example

명령>select key from set.myset* where key in ('myset1','myset2')
결과> 0) key
1) myset1
2) myset2
명령>select * from set.myset1 where value in ('mem10','mem30');
결과> 0) key|value
1) myset1|mem10
2) myset1|mem30
명령>select * from set.myset2 where value in (20,40);
결과> 0) key|value
1) myset1|20
2) myset1|40

NOT IN

Example

명령>select key from set.myset* where key not in ('myset1','myset2');
결과> 0) key
1) myset3
2) myset4
명령>select * from set.myset1 where value not in ('mem10','mem30');
결과> 0) key|value
1) myset1|mem40
2) myset1|mem20
3) myset1|mem50
명령>select * from set.myset2 where value not in (20,40);
결과> 0) key|value
1) mylist2|10
2) mylist2|30
3) mylist2|50

GROUP BY

Example

명령> select key,count(*) from set.myset* group by key;
결과> 0) key|count(*)
1) myset1|5
2) myset2|5
3) myset3|5
4) myset4|5
명령> select key,min(value),max(value) from set.myset* group by key;
결과> 0) key|min(value)|max(value)
1) myset1|mem10|mem50
2) myset2|10|50
3) myset3|12.3|56.7
4) myset4|USER-A01|USER-C01

<< Select List Select Set Select ZSet >>

Email 답글이 올라오면 이메일로 알려드리겠습니다.