Let's say we have a table team_person as below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
+======+===========+
| team |    person |
+======+===========+
|   A  |      John |
+------+-----------+
|   B  |     Smith |
+------+-----------+
|   A  |    Walter |
+------+-----------+
|   A  |     Louis |
+------+-----------+
|   C  | Elizabeth |
+------+-----------+
|   B  |     Wayne |
+------+-----------+
 
 
CREATE TABLE team_person AS SELECT 'A' team, 'John' person
UNION ALL  SELECT 'B' team,  'Smith' person
UNION ALL  SELECT 'A' team,  'Walter' person
UNION ALL  SELECT 'A' team,  'Louis' person
UNION ALL  SELECT 'C' team,  'Elizabeth' person
UNION ALL  SELECT 'B' team,  'Wayne' person;
cs

 

To select the table team_person with additional row_number column, either

1
2
SELECT @row_no := @row_no+1 AS row_number, team, person
FROM team_person, (SELECT @row_no := 0) t;
cs

* rownum 초기화 : (SELECT @row_no := 0) t;

 

OR

1
2
3
SET @row_no := 0;
SELECT  @row_no := @row_no + 1 AS row_number, team, person
FROM team_person;
cs

 

will output the result below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
+============+======+===========+
| row_number | team |    person |
+============+======+===========+
|          1 |   A  |      John |
+------------+------+-----------+
|          2 |   B  |     Smith |
+------------+------+-----------+
|          3 |   A  |    Walter |
+------------+------+-----------+
|          4 |   A  |     Louis |
+------------+------+-----------+
|          5 |   C  | Elizabeth |
+------------+------+-----------+
|          6 |   B  |     Wayne |
+------------+------+-----------+
cs

 

Finally, if we want to get the row_number group by column team

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
SELECT @row_no := IF(@prev_val = t.team, @row_no + 11) AS row_number
   ,@prev_val := t.team AS team
   ,t.person  
FROM team_person t,
  (SELECT @row_no := 0) x,
  (SELECT @prev_val := '') y
ORDER BY t.team ASC,t.person DESC; 
 
+============+======+===========+
| row_number | team |    person |
+============+======+===========+
|          1 |   A  |    Walter |
+------------+------+-----------+
|          2 |   A  |     Louis |
+------------+------+-----------+
|          3 |   A  |      John |
+------------+------+-----------+
|          1 |   B  |     Wayne |
+------------+------+-----------+
|          2 |   B  |     Smith |
+------------+------+-----------+
|          1 |   C  | Elizabeth |
+------------+------+-----------+
cs

 

출처 : riptutorial.com/mysql/example/19572/row-number-and-group-by-using-variables-in-select-statement

 

MySQL - Row Number and Group By using variables in Select Statement | mysql Tutorial

mysql documentation: Row Number and Group By using variables in Select Statement

riptutorial.com

developer-jjun.tistory.com/23

 

[MySQL] ROWNUM을 사용하여 번호매기기

MySQL에서 Oracle처럼 ROWNUM 사용법 SET구문을 사용하여 ROWNUM 값을 초기화 후 조회 SET @rownum:=0; SELECT @rownum:=@rownum+1, b.* FROM buyingboard b WHERE절에서 초기화 SELECT @rownum:=@rownum+1, b.*..

developer-jjun.tistory.com

 

 

Posted by 셋부터넷
,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
SELECT 
    DATE_FORMAT(m1, '%Y-%m') AS standardDate
FROM(
    SELECT 
        ('2020-01-11' - INTERVAL DAYOFMONTH('2020-01-11')-1 DAY) +INTERVAL m MONTH as m1
    FROM(
        SELECT @rownum:=@rownum+1 as m FROM
        (SELECT 1 union SELECT 2 union SELECT 3 union SELECT 4) t1,
        (SELECT 1 union SELECT 2 union SELECT 3 union SELECT 4) t2,
        (SELECT 1 union SELECT 2 union SELECT 3 union SELECT 4) t3,
        (SELECT 1 union SELECT 2 union SELECT 3 union SELECT 4) t4,
        (SELECT @rownum:=-1) t0
    ) d1
) d2 
WHERE m1 <= '2020-09-30' 
ORDER BY m1
cs

 

참고 : stackoverrun.com/ko/q/4011597

 

mysql에서 두 날짜 사이의 월 목록을 얻는 방법

mysql에서 두 날짜 사이의 월 목록을 가져 왔습니다. For Example:My Input is From date 23-01-2013 To Date 01-04-2014 Output Should be Jan 2013, Feb 2013, March 2013, . . . Jan 2014, Feb 2014, Mar 2014, Apr ...

stackoverrun.com

 

 

Posted by 셋부터넷
,

어느날 아래와 같은 구조를  가진 테이블을

 idx

area1 

area2 

area3 

area4 

area5 

 1

 10

20 

30 

40 

50 

 2

 11

21 

31 

41 

51 

 

 

아래와 같이 변경 해야 하는 경우가 닥치고 말았다

no

position 

value 

 1

 area1

 10

 1

 area2

 20

 1

 area3

 30

 1

 area4

 40

 1

 area5

 50

 2

 area1

 11

 2

 area2

 21

 2

 area3

 31

 2

 area4

 41

 2

 area5

 51

 

해결책 :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
SELECT
    `no`,
    CASE 
        WHEN x=1 THEN 'area1'
        WHEN x=2 THEN 'area2'
        WHEN x=3 THEN 'area3'
        WHEN x=4 THEN 'area4'
        WHEN x=5 THEN 'area5'
    END `position`,
 
    CASE 
        WHEN x=1 THEN area1
        WHEN x=2 THEN area2
        WHEN x=3 THEN area3
        WHEN x=4 THEN area4
        WHEN x=5 THEN area5
    END `value
FROM (
    select * from default_data a,
    (
        select 1 AS x
        union all select 2 AS x
        union all select 3 AS x
        union all select 4 AS x
        union all select 5 AS x
    ) b
)
ORDER BY `NO`, `NUMBERS` ASC;
cs

 

[출처] Mysql 가로 컬럼 형태를 세로 rows 형태 로 변경|작성자 RS

Posted by 셋부터넷
,

MySQL에서 특정날짜 사이의 날짜 목록 모두 가져오기

1
2
3
4
5
6
7
8
9
select * from 
(select adddate('2010-01-01',t4.i*10000 + t3.i*1000 + t2.i*100 + t1.i*10 + t0.i) selected_date from
 (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t0,
 (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t1,
 (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t2,
 (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t3,
 (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t4) v
where selected_date 
between '2018-09-10' and '2018-10-31'
cs

 

 

참고 : stackoverflow.com/questions/9295616/how-to-get-list-of-dates-between-two-dates-in-mysql-select-query

 

How to get list of dates between two dates in mysql select query

I want list of dates lies between two dates by select query. For example: If i give '2012-02-10' and '2012-02-15' I need the result. date ---------- 2012-02-10 2012-02-11 2012-02-12 2012-0...

stackoverflow.com

 

 

Posted by 셋부터넷
,

Oracle의 계층형쿼리(Connect By Prior~)를 MySQL로 대체하여 표현해보자~

 

ex) 계층구조를 갖는 메뉴데이터를 계층쿼리로 표현

 

-------- 테이블 생성

CREATE TABLE 'tb_addr' (
    'a_idx' INT NOT NULL AUTO_INCREMENT COMMENT '주소번호',
    'a_pidx' INT NOT NULL COMMENT '부모 주소번호',
    'a_title' VARCHAR(100) NOT NULL COMMENT '주소명'
    PRIMARY KEY ('a_idx')
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

 

-------- 쿼리(메뉴 계층구조 표현 및 정렬)

SELECT
  o_title, a_idx, IFNULL(lTB.a_pidx, 0) as a_pidx, IFNULL(cnt, 0) as sub_cnt, path
FROM(
  WITH RECURSIVE tmp1 AS
  (
      SELECT 
        a_idx, a_title, a_title as o_title, a_pidx, 
        a_title AS path,
        CONCAT(a_idx,',',a_title) AS sort_str,
        1 AS lvl
      FROM tb_addr WHERE a_pidx is null
      UNION ALL
      SELECT 
        e.a_idx, e.a_title, e.a_title as o_title, e.a_pidx, 
        CONCAT(t.path,'/',e.a_title) AS path, 
        CONCAT(t.sort_str,'>',CONCAT(e.a_idx,',',e.a_title)) AS sort_str, 
        t.lvl+1 AS lvl 
      FROM tmp1 t 
      JOIN tb_addr e 
      ON t.a_idx = e.a_pidx
  )
  SELECT 
    CONCAT(REPEAT(' ', lvl*4), a_title) as a_title, o_title,
    a_idx, a_pidx,
    path, sort_str, lvl
  FROM tmp1 
) lTB
LEFT JOIN(
  select a_pidx, count(a_pidx) as cnt from tb_addr group by a_pidx
) AS rTB
ON(lTB.a_idx = rTB.a_pidx)
ORDER BY sort_str

Posted by 셋부터넷
,