2017-09-18 1 views
0

테이블 superstatistics_counter에 100 000 개의 행을 생성 할 SQL 쿼리가 있습니다. 인터넷에서이 사실을 발견했습니다. 그러나 그것은 저에게 마법입니다. 나는 SQL 쿼리에 대해 기본적인 이해를하지 못했다. 나는 봤지만 아직 내게 불분명하다 ...내가 이해하지 못하는 SQL 쿼리

괄호 안의 진술은 무엇을 의미합니까? t, t2은 무엇을 의미합니까? @열?

INSERT INTO superstatistics_counter(nid, totalcount, daycount,timestamp) SELECT @row := @row + 1 as row, 100, 50, NOW() FROM 
(select 0 union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) t, 
(select 0 union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) t2, 
(select 0 union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) t3, 
(select 0 union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) t4, 
(select 0 union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) t5, 
(SELECT @row:=0) t6 

감사합니다.

+1

암시 적'JOIN' 구문 ('FROM' 절의',')을 통해'CROSS JOIN'을하고 있습니다. 기본적으로 모든 하위 선택 문의 cartesan 제품을 가져와 0에서 99,999까지의 모든 숫자를 산출합니다. 't # '는 각 하위 선택의 별명입니다. – Siyual

+0

t는 트랜잭션 번호로 간단히 참조합니다 –

+1

t, t2 등은 괄호 안에있는 이름의 별칭입니다. @는 MySql의 변수 이름을 짓는 데 사용됩니다. – MarioZ

답변

2

마지막 줄 (SELECT @row:=0) t6은 변수 @row를 0으로 초기화 한 다음 첫 번째 줄의 루프 @row := @row + 1을 매번 1 씩 증가시킵니다. t ~ t5은 각각 10 개의 행을 갖는 교차 결합됩니다. 따라서 10 * 10 * 10 * 10 * 10 = 100000 행을 생성합니다. 100000 개의 행에서 ID가 1에서 100000으로 변경되고 @row에서부터입니다.

0

각 행에서 0-9를 선택하면 각 열에 대해 10 개의 레코드가 생성됩니다. 그래서 당신은 0에서 9까지의 값을 갖는 5 개의 열 (각각 t, t2, t3, t4, t5로 별칭이 지정됩니다.)이 교차 결합되어 10 만 개의 레코드를 만듭니다.

그리고 @를 사용하여 변수를 정의합니다.

@row : = @row + 1은 +1 증가분의 루프로 실행 중입니다.

관련 문제