2011-09-13 5 views
18

썸네일 파일이 첨부 된 파일을 선택하는 쿼리가 있는데 썸네일이 첨부되지 않은 파일도 가져와야합니다.다른 열에 대한 MySql SELECT 유니온?

나의 현재 SQL 쿼리 내가 일반적으로 단지

(SELECT node.title, node.nid, files.fid, files.filepath, content_type_mobile_event.field_date_value, content_type_mobile_event.field_movie_shorts_value, content_type_mobile_event.field_director_value, content_type_mobile_event.field_length_value, content_type_mobile_event.field_movie_type_value, content_type_mobile_event.field_year_value, content_type_mobile_event.field_event_type_value, content_type_mobile_event.field_movie_location_value, content_type_mobile_event.field_movie_desc_value, content_type_mobile_event.field_movie_id_value, content_type_mobile_event.field_movie_thumb_fid, content_type_mobile_event.field_movie_trailer_url FROM node, content_type_mobile_event, files WHERE node.nid=content_type_mobile_event.nid AND content_type_mobile_event.field_movie_thumb_fid=files.fid ORDER BY content_type_mobile_event.field_date_value ASC) 
UNION 
(SELECT node.title, node.nid, content_type_mobile_event.field_date_value, content_type_mobile_event.field_movie_shorts_value, content_type_mobile_event.field_director_value, content_type_mobile_event.field_length_value, content_type_mobile_event.field_movie_type_value, content_type_mobile_event.field_year_value, content_type_mobile_event.field_event_type_value, content_type_mobile_event.field_movie_location_value, content_type_mobile_event.field_movie_desc_value, content_type_mobile_event.field_movie_id_value, content_type_mobile_event.field_movie_thumb_fid, content_type_mobile_event.field_movie_trailer_url FROM node, content_type_mobile_event WHERE node.nid=content_type_mobile_event.nid AND content_type_mobile_event.field_movie_thumb_fid!=1 ORDER BY content_type_mobile_event.field_date_value ASC) 

할 것이라고 나는 또한

SELECT node.title, node.nid, content_type_mobile_event.field_date_value, content_type_mobile_event.field_movie_shorts_value, content_type_mobile_event.field_director_value, content_type_mobile_event.field_length_value, content_type_mobile_event.field_movie_type_value, content_type_mobile_event.field_year_value, content_type_mobile_event.field_event_type_value, content_type_mobile_event.field_movie_location_value, content_type_mobile_event.field_movie_desc_value, content_type_mobile_event.field_movie_id_value, content_type_mobile_event.field_movie_thumb_fid, content_type_mobile_event.field_movie_trailer_url FROM node, content_type_mobile_event WHERE node.nid=content_type_mobile_event.nid AND content_type_mobile_event.field_movie_thumb_fid!=1 ORDER BY content_type_mobile_event.field_date_value ASC 

을 얻을 필요가

SELECT node.title, node.nid, files.fid, files.filepath, content_type_mobile_event.field_date_value, content_type_mobile_event.field_movie_shorts_value, content_type_mobile_event.field_director_value, content_type_mobile_event.field_length_value, content_type_mobile_event.field_movie_type_value, content_type_mobile_event.field_year_value, content_type_mobile_event.field_event_type_value, content_type_mobile_event.field_movie_location_value, content_type_mobile_event.field_movie_desc_value, content_type_mobile_event.field_movie_id_value, content_type_mobile_event.field_movie_thumb_fid, content_type_mobile_event.field_movie_trailer_url FROM node, content_type_mobile_event, files WHERE node.nid=content_type_mobile_event.nid AND content_type_mobile_event.field_movie_thumb_fid=files.fid ORDER BY content_type_mobile_event.field_date_value ASC 

입니다 그러나 문제는 두 번째가있다이다 다른 열 집합 (빼기 파일 * 부분)

나는이 일을하는 방법을 이해할 수 없다.

+4

줄 바꿈을 사용할 수 있음을 알고 있습니까? 별칭도 도움이 될 수 있습니다. :) – GolezTrol

답변

38

다른 필드가 다른 의미를 가지고있는 경우에도 동일한 위치에 표시 할 수 없으며 반환하지 않아야합니다. 당신은 그러나이 같은, 당신의 필드에 null을 추가하여 '빈칸을 채워'할 수

select id, name, date, null as userid, 'A' as recordtype from table1 
union all 
select id, name, null /*as date*/, userid, 'B' as recordtype from table2 

당신은 첫 번째 선택에서 널 별칭을 제공 할 수 있습니다. 명확하게하기 위해 두 번째 선택 영역에 별칭을 추가 할 수 있지만 사용되지는 않습니다. 나중에 레코드 유형을 구별하는 데 사용할 수있는 상수 값을 사용할 수도 있습니다. 선택 A는 열이없는 경우

10

, 단순히

table A (colA, ColB, ColC) 

table B (colA, ColD, ColE) 

select colA, ColB, ColC, null, null from table A 
union 
select colA, null, null, colD, colE from table B 

은 당신이 일치하는 각 열은 같은 데이터 유형

+0

이 방법을 지적 해 주셔서 감사합니다. 각 테이블에 대해 동일한 수의 열을 쿼리하지만 각 열의 한 열에는 서로 다른 데이터 형식이 있습니다. nulls는 이것을 정렬하고 내가 필요한 결과를 얻을 수있었습니다. 감사! –

5

당신이 할 수있는 가짜로 있는지 확인해야 null 값을 사용 노조 할 칼럼

예 :

select x, y from A 
    union 
    select z, null from B