2016-07-23 3 views
3

외래 키로 공통 열 comm_name을 갖는 3 개의 테이블 table_1, table_2 및 table_3이 있습니다. 이러한 3 개의 테이블을 조인하여 최대 열 수 data_id를 찾고 싶습니다. 노동 조합이 세 테이블을 찾아 최대에 Data_ID, 즉 : 리턴 9테이블 조인 및 최대 열 값 찾기

내가 좋아하는 시도 : 조건 comm_name 짧은

사용하여 위치를

SELECT max(data_id) FROM ((SELECT table_1.data_id FROM table_1 where comm_name='aa') UNION(SELECT table_2.data_id FROM table_2 where comm_name='aa') UNION(SELECT table_3.data_id FROM table_2 where comm_name='aa')); 

그러나 그 보여주는 오류

An expression was expected. (near "(" at position 26) 
Unexpected token. (near "(" at position 26) 
Unexpected token. (near "(" at position 27) 
This type of clause was previously parsed. (near "SELECT" at position 29) 
This type of clause was previously parsed. (near "SELECT" at position 125) 
This type of clause was previously parsed. (near "SELECT" at position 220) 

enter image description here

답변

3

연합 (EU) 연산자를 사용하면 당신이 (테스트하지) 조회 수 1 개 데이터 세트가하는 데 도움이 될 것입니다

select max(data_id) from (select data_id from table_1 union select data_id from table_2 union select data_id from table_3) 
0

는 시도를 조건 comm_name을 확인 할 위치를 추가하는 방법을

SELECT Name, MAX(data_id) as MaxId 
    FROM 
    (
     SELECT data_id 
     FROM table1 
     UNION ALL 
     SELECT data_id 
     FROM table2 
     UNION ALL 
     SELECT data_id 
     FROM table3 
    ); 
+0

는 세 개의 테이블에 동일이 –