2011-09-13 33 views
18

我有一個選擇查詢選擇文件附加縮略圖文件,我也需要得到那些沒有附加縮略圖。MySql SELECT union for不同的列?

我現在的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 

我還需要獲得

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) 
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) 

但問題是,第二個有一個不同的一組列(減去文件。*部分)

我不能爲我的生活弄清楚如何做到這一點。

+4

你知道你可以使用換行符,不是嗎?別名也可以提供幫助。 :) – GolezTrol

回答

38

如果你有不同的字段也有不同的含義,你不能也不應該將它們放回到相同的位置。但是,您可以「填補空白」加入空到你的領域,如:

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 

您可以在第一選擇空提供的別名。您可以在第二個選擇中添加別名以清晰起見,但不會使用它。你甚至可以使用常量值,以後可以用它來區分記錄類型。

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

感謝您指出這種方法。我有相同數量的列來查詢每個表,但每列中有一列具有不同的數據類型。空值幫助解決這個問題,並得到我需要的結果。謝謝! –

5

你可以假的一列做聯合

例如。

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