마리아에서 테이블 2개를 완전히 연결하다DB
테이블이 두 개 있습니다.
Table1
Year,Month, data
2017,1,2
2018,2,10
Table2
Year,Month,data2
2017,1,5
2019,2,2
저는 테이블을 하나의 테이블로 통합하려고 합니다.여기서 양쪽 테이블에서 모든 행을 얻습니다.
Year,Month,data,data2
2017 ,1,2,5
2018,2,10,NULL
2019,2,NULL,2
여기서는 표준 외부 조인은 작동하지 않을 것 같고 Union ALL도 사용할 수 없습니다.이 작업을 수행하기 위한 외부 조인 쿼리가 있습니까?
양쪽 테이블에서 모든 연도와 월을 가져오려면 UNION을 사용하고 표 1 및 표 2와 관련짓기 위해서는 왼쪽 조인을 사용해야 합니다.
select a.Year , a.Month, b.data, c.data2
from (
select Year,Month
from Table1
union
select Year,Month
from Table2
) a
left join table1 b on a.Year = b.Year and a.month = b. month
left join table2 c on a.Year = c.Year and a.month = c. month
네가 정말 원하는 건full join. 자주 효과가 있는 방법 중 하나는union all그리고.group by:
select year, month, max(data) as data, max(data2) as data2
from ((select year, month, data, null as data2
from table1
) union all
(select year, month, null, data2
from table2
)
) t
group by year, month;
언급URL : https://stackoverflow.com/questions/58684018/full-join-2-tables-in-mariadb
'source' 카테고리의 다른 글
| Python이 해석되면 .pyc 파일이 뭐죠? (0) | 2022.11.12 |
|---|---|
| jQuery와 비동기적으로 파일을 업로드하려면 어떻게 해야 합니까? (0) | 2022.11.12 |
| System.out.println()을 짧게 하는 방법 (0) | 2022.11.12 |
| 특징과 인터페이스 (0) | 2022.11.12 |
| 레퍼런스:MySQL 확장자를 사용하는 완벽한 코드 샘플은 무엇입니까? (0) | 2022.11.02 |