2017-05-22 1 views
0

sample.db라는 데이터베이스를 만들었습니다.sqlite 명령을 사용하여 2 개의 테이블 조인

내가 3 열을 기반으로 두 테이블에 가입 할 -test와 TEST1 (데이터베이스 테이블)

2 개 CSV 파일 가져 왔습니다 - 타임 스탬프, 위도와 경도를. 당신이 볼 수 있듯이

시험은

o3,pm,co,so2,no2,longitude,latitude,timestamp  
61,82,52,49,54,10.2466,56.2091,1410693900  
66,83,51,44,51,10.2466,56.2091,1410694200  
68,80,54,41,56,10.2466,56.2091,1410694500  
66,79,53,42,59,10.2466,56.2091,1410694800  
71,75,57,45,61,10.2466,56.2091,1410695100  
75,75,60,50,61,10.2466,56.2091,1410695400 
122,97,172,40,53,10.2507,56.2026,1412101200 

TEST1

status,avgMeasuredTime,avgSpeed,extID,medianMeasuredTime,TIMESTAMP,vehicleCount,_id,REPORT_ID,Lat1,Long1,Lat2,Long2,Distance between 2 points,duration of measurements,ndt in kmh  
OK,61,60,668,61,1410693900,4,20746723,158324,56.2091,10.2466,56.2258,10.1166,1030,52,71 
OK,61,60,668,61,1410694200,1,20747172,158324,56.2091,10.2466,56.2258,10.1166,1030,52,71 
OK,63,58,668,63,1410694500,3,20747545,158324,56.2091,10.2466,56.2258,10.1166,1030,52,71 
OK,71,52,668,71,1410694800,6,20747994,158324,56.2091,10.2466,56.2258,10.1166,1030,52,71 
OK,67,55,668,67,1410695100,5,20748443,158324,56.2317,10.1050,56.2091,10.2466,1030,52,71 
OK,62,59,668,62,1410695400,6,20748892,158324,56.2317,10.1050,56.2091,10.2466,1030,52,71 
OK,67,55,668,67,1412101200,13,20749341,158324,56.2317,10.1050,56.2026,10.2507,1030,52,71 

예상 출력

ozone,particullate_matter,carbon_monoxide,sulfure_dioxide,nitrogen_dioxide,longitude,latitude,timestamp,avgMeasuredTime,avgSpeed,medianMeasuredTime,Distance between 2 points,duration of measurements,ndt in kmh 
61,82,52,49,54,10.2466,56.2091,1410693900,61,60,61,1030,52,71 
66,83,51,44,51,10.2466,56.2091,1410694200,61,60,61,1030,52,71 
68,80,54,41,56,10.2466,56.2091,1410694500,63,58,63,1030,52,71 
66,79,53,42,59,10.2466,56.2091,1410694800,71,52,71,1030,52,71 
71,75,57,45,61,10.2466,56.2091,1410695100,67,55,67,1030,52,71 
75,75,60,50,61,10.2466,56.2091,1410695400,62,59,62,1030,52,71 
122,97,172,40,53,10.2507,56.2026,1412101200,67,55,67,1030,52,71 

, 나는 타임 스탬프, 위도와 경도에 따라 가입하려는. table2 (test1)에는 두 개의 경도 값이 있습니다. 그래서 table2의 경도 값 중 하나와 일치하는 table1의 경도를 비교하고 싶습니다. 마찬가지로 위도. 내가

select * 
from test 
join test1 
    on test.timestamp=test1.TIMESTAMP and 
    test.longitude=test1.Long1 or 
    test.longitude=test1.Long2 and 
    test.latitude=test1.Lat1 or 
    test.latitude=test1.Lat2; 

을 시도 무엇

또한 나는 새 테이블에 결과를 저장할.

저는 sqlite를 처음 사용합니다. 그래서 INNER JOIN에 대한 :)

+1

올바르게 태그를 지정하십시오. 너 뭐 해봤 니? 나는 당신이 시도하지 못한 것을 보여주는 SQL 문을 보지 못했다. –

+0

select *에서 test join test1 on (test.latitude = test1.latitude 및 test.longitude = test1.longitude 및 test.timestamp = test1.timestamp) – JYoThI

+0

주석에 답변을 넣지 마십시오. 특히 그들이 맞지 않을 때. –

답변

1

조건을 그룹화하려면 괄호를 사용하십시오. 당신은 타임 스탬프가 일치하는 행을 원합니다 (위도/경시 쌍 중 하나가 OR 위도/경도 쌍 쌍과 일치합니다)

select * 
from test 
join test1 
    on test.timestamp = test1.TIMESTAMP and 
    ((test.longitude = test1.Long1 and test.latitude = test1.Lat1) or 
    (test.longitude = test1.Long2 and test.latitude = test1.Lat2)); 
+0

이것은 완벽하게 작동합니다! 새 테이블에 결과를 저장하는 방법을 알려주시겠습니까? – Mikasa

+0

SELECT 구문에서 INSERT를 찾습니다. 기본적으로, 목표 테이블의 컬럼을 리턴하는 SELECT.을 작성한 후이를 INSERT.의 데이터 소스로 사용하십시오. –

+0

대단히 감사합니다. – Mikasa

-1

SQL 명령을 도와주세요 : 다른 컬럼에 대한

SELECT latitude 
FROM test 
INNER JOIN test1 ON test.latitude = test1.latitude; 

반복합니다.