2011-09-13 4 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 

あなたが最初の選択でヌルのエイリアスを提供することができます。明快にするために2番目の選択にエイリアスを追加できますが、使用されません。後でレコードタイプを区別するために使用できる定数も使用できます。選択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

このメソッドを指摘してくれてありがとう。私は、各テーブルを照会する列の数は同じですが、それぞれの列のデータ型が異なります。 nullはこれを整理し、必要な結果を得るのに役立ちました。ありがとう! –

5

することができます偽物であることを確認する必要があり、null値を使用しますユニオンを行う列

例:

select x, y from A 
    union 
    select z, null from B 
関連する問題