SELECT ZSet


SELECT로 ZSet 데이터를 조회

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

테스트 데이터 입력

Example

명령>zadd myzset1 10 value10 20 value20 30 value30 40 value40 50 value50
결과>5
명령>zadd myzset2 12.3 mem10 23.4 mem20 34.5 mem30 45.6 mem40 56.7 mem50
결과>5
명령>zadd myzset2 67.8 mem60 78.9 mem70 89.1 mem80 99.9 mem90 123.4 mem100
결과>5

SELECT

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

FROM

FROM zset.*
FROM zset.myzset*
FROM zset.myzset1

SELECT * FROM SET.MYZSET*

ZSet의 키와 스코어(score), 값(value)를 조회한다.

Example

명령>select * from zset.myzset*;
결과>   0) key|score|value
  1) myzset1|10.0|value10
  2) myzset1|20.0|value20
  3) myzset1|30.0|value30
  4) myzset1|40.0|value40
  5) myzset1|50.0|value50
  6) myzset2|12.3|mem10
  7) myzset2|23.4|mem20
  8) myzset2|34.5|mem30
  9) myzset2|45.6|mem40
10) myzset2|56.7|mem50
11) myzset2|67.8|mem60
12) myzset2|78.9|mem70
13) myzset2|89.1|mem80
14) myzset2|99.9|mem90
15) myzset2|123.4|mem100
명령>select * from zset.myzset1;
결과> 0) key|score|value
1) myzset1|10.0|value10
2) myzset1|20.0|value20
3) myzset1|30.0|value30
4) myzset1|40.0|value40
5) myzset1|50.0|value50
명령>select key from zset.myzset*;
결과> 0) key
1) myzset1
2) myzset2

ORDER BY

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

Example

명령>select score,value from zset.myzset1 order by value;
결과> 0) score|value
1) 10.0|value10
2) 20.0|value20
3) 30.0|value30
4) 40.0|value40
5) 50.0|value50
명령>select score,value from zset.myzset1 order by value desc;
결과> 0) score|value
1) 50.0|value50
2) 40.0|value40
3) 30.0|value30
4) 20.0|value20
5) 10.0|value10

Score

Example

명령>select * from zset.myzset* order by score;
결과>   0) key|score|value
  1) myzset1|10.0|value10
  2) myzset2|12.3|mem10
  3) myzset1|20.0|value20
  4) myzset2|23.4|mem20
  5) myzset1|30.0|value30
  6) myzset2|34.5|mem30
  7) myzset1|40.0|value40
  8) myzset2|45.6|mem40
  9) myzset1|50.0|value50
10) myzset2|56.7|mem50
11) myzset2|67.8|mem60
12) myzset2|78.9|mem70
13) myzset2|89.1|mem80
14) myzset2|99.9|mem90
15) myzset2|123.4|mem100

LIMIT

출력 행 수를 제한한다.

Example

명령>select * from zset.myzset2 limit 5;
결과> 0) key|score|value
1) myzset2|12.3|mem10
2) myzset2|23.4|mem20
3) myzset2|34.5|mem30
4) myzset2|45.6|mem40
5) myzset2|56.7|mem50
명령>select * from zset.myzset2 order by value desc limit 5;
결과> 0) key|score|value
1) myzset2|99.9|mem90
2) myzset2|89.1|mem80
3) myzset2|78.9|mem70
4) myzset2|67.8|mem60
5) myzset2|56.7|mem50

LIMIT OFFSET

몇 행 건너 출력

Example

명령>select * from zset.myzset2 limit 5 offset 3; --> 3행 건너 5행 출력
결과> 0) key|score|value
1) myzset2|45.6|mem40
2) myzset2|56.7|mem50
3) myzset2|67.8|mem60
4) myzset2|78.9|mem70
5) myzset2|89.1|mem80
명령>select * from zset.myzset2 limit 3, 5; --> 3행 건너 5행 출력
결과> 0) key|score|value
1) myzset2|45.6|mem40
2) myzset2|56.7|mem50
3) myzset2|67.8|mem60
4) myzset2|78.9|mem70
5) myzset2|89.1|mem80

FUNCTIONS

COUNT()

개수를 조회한다.

Example

명령>select count(*) from zset.myzset*;
결과> 0) count(*)
1) 15
명령>select count(key) from zset.myzset*;
결과> 0) count(key)
1) 2
명령>select key, count(*) from zset.myzset* group by key;
결과> 0) key|count(*)
1) myzset1|5
2) myzset2|10

MAX(), MIN()

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

Example

명령>select key,min(score),max(score),min(value),max(value) from zset.myzset2;
결과> 0) key|min(score)|max(score)|min(value)|max(value)
1) myzset2|12.3|123.4|mem10|mem90
명령> select key,min(score),max(score),min(value),max(value) from zset.myzset* group by key;
결과> 0) key|min(score)|max(score)|min(value)|max(value)
1) myzset1|10.0|50.0|value10|value50
2) myzset2|12.3|123.4|mem10|mem90

숫자 함수

SUM(), AVG()

합계, 평균을 구한다.

Example

명령>select sum(score) from zset.myzset2;
결과> 0) sum(value)
1) 631.6
명령>select avg(score) from zset.myzset2;
결과> 0) avg(value)
1) 63.16

기타 함수

group_concat

Example

명령>select key,group_concat(score) from zset.myzset1;
결과> 0) key|group_concat(score)
1) myzset1|10.0,20.0,30.0,40.0,50.0
명령>select key,group_concat(value) from zset.myzset1;
결과> 0) key|group_concat(value)
1) myzset1|value10,value20,value30,value40,value50
명령>select key,group_concat(score),group_concat(value) from zset.myzset1;
결과> 0) key|group_concat(score)|group_concat(value)
1) myzset1|10.0,20.0,30.0,40.0,50.0|value10,value20,value30,value40,value50


WHERE 조건

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

스코어(score) 비교: 숫자

Example

명령>select * from zset.myzset1 where score = 30;
결과> 0) key|score|value
1) myzset1|30.0|value30
명령>select * from zset.myzset1 where score > 30;
결과> 0) key|score|value
1) myzset1|40.0|value40
2) myzset1|50.0|value50
명령>select * from zset.myzset1 where score < 30;
결과> 0) key|score|value
1) myzset1|10.0|value10
2) myzset1|20.0|value20
명령>select * from zset.myzset1 where score != 30;
결과> 0) key|score|value
1) myzset1|10.0|value10
2) myzset1|20.0|value20
3) myzset1|40.0|value40
4) myzset1|50.0|value50

값(value) 비교: 문자

Example

명령>select * from zset.myzset1 where value > 'value30';
결과> 0) key|score|value
1) myzset1|40.0|value40
2) myzset1|50.0|value50
명령>select * from zset.myzset1 where value >= 'value30';
결과> 0) key|score|value
1) myzset1|30.0|value30
2) myzset1|40.0|value40
3) myzset1|50.0|value50
명령>select * from zset.myzset1 where value != 'value30';
결과> 0) key|score|value
1) myzset1|10.0|value10
2) myzset1|20.0|value20
3) myzset1|40.0|value40
4) myzset1|50.0|value50

AND, OR

Example

명령>select * from zset.myzset1 where score >= 20 and score < 40;
결과> 0) key|score|value
1) myzset1|20.0|value20
2) myzset1|30.0|value30
명령>select * from zset.myzset1 where score <= 20 or score > 40;
결과> 0) key|score|value
1) myzset1|10.0|value10
2) myzset1|20.0|value20
3) myzset1|50.0|value50

BETWEEN

Example

명령>select * from zset.myzset1 where score between 20 and 40;
결과> 0) key|score|value
1) myzset1|20.0|value20
2) myzset1|30.0|value30
3) myzset1|40.0|value40
명령>select * from zset.myzset1 where score not between 20 and 40;
결과> 0) key|score|value
1) myzset1|10.0|value10
2) myzset1|50.0|value50

LIKE

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

Example

명령>zadd myzset3 10 USER-A01 20 USER-A02 30 USER-B01 40 USER-B02 50 USER-C01
결과> 5
명령>select * from zset.myzset3 where value like 'user-a%';
결과> 0) key|score|value
1) myzset3|10.0|USER-A01
2) myzset3|20.0|USER-A02
명령>select * from zset.myzset3 where value like '%01%';
결과> 0) key|score|value
1) myzset3|10.0|USER-A01
2) myzset3|30.0|USER-B01
3) myzset3|50.0|USER-C01

NOT LIKE

Example

명령>select * from zset.myzset3 where value not like 'user-a%';
결과> 0) key|score|value
1) myzset3|30.0|USER-B01
2) myzset3|40.0|USER-B02
3) myzset3|50.0|USER-C01
명령>select * from zset.myzset3 where value not like '%01%';
결과> 0) key|score|value
1) myzset3|20.0|USER-A02
2) myzset3|40.0|USER-B02

GLOB

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

Example

명령>select * from zset.myzset3 where value glob 'USER-A*';
결과> 0) key|score|value
1) myzset3|10.0|USER-A01
2) myzset3|20.0|USER-A02
명령>select * from zset.myzset3 where value glob '*01*';
결과> 0) key|score|value
1) myzset3|10.0|USER-A01
2) myzset3|30.0|USER-B01
3) myzset3|50.0|USER-C01

NOT GLOB

Example

명령>select * from zset.myzset3 where value not glob 'USER-A*';
결과> 0) key|score|value
1) myzset3|30.0|USER-B01
2) myzset3|40.0|USER-B02
3) myzset3|50.0|USER-C01
명령>select * from zset.myzset3 where value not glob '*01*';
결과> 0) key|score|value
1) myzset3|20.0|USER-A02
2) myzset3|40.0|USER-B02

IN

Example

명령>select key from zset.myzset* where key in ('myzset1','myzset2')
결과> 0) key
1) myzset1
2) myzset2
명령>select * from zset.myzset1 where score in (10,30);
결과> 0) key|score|value
1) myzset1|10.0|value10
2) myzset1|30.0|value30
명령>select * from zset.myzset1 where value in ('value10','value30');
결과> 0) key|score|value
1) myzset1|10.0|value10
2) myzset1|30.0|value30

NOT IN

Example

명령>select key from zset.myzset* where key not in ('myzset1','myzset2');
결과> 0) key
1) myzset3
명령>select * from zset.myzset1 where score not in (10,30);
결과> 0) key|score|value
1) myzset1|20.0|value20
2) myzset1|40.0|value40
3) myzset1|50.0|value50
명령>select * from zset.myzset1 where value not in ('value10','value30');
결과> 0) key|score|value
1) myzset1|20.0|value20
2) myzset1|40.0|value40
3) myzset1|50.0|value50

GROUP BY

Example

명령> select key,count(*) from zset.myzset* group by key;
결과> 0) key|count(*)
1) myzset1|5
2) myzset2|10
3) myzset3|5
명령> select key,min(score),max(score) from zset.myzset* group by key;
결과> 0) key|min(score)|max(score)
1) myzset1|10.0|50.0
2) myzset2|12.3|123.4
3) myzset3|10.0|50.0
명령> select key,min(value),max(value) from zset.myzset* group by key;
결과> 0) key|min(value)|max(value)
1) myzset1|value10|value50
2) myzset2|mem10|mem90
3) myzset3|USER-A01|USER-C01

<< Select Set Select ZSet Select Hash >>

조회수 :

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