2011-09-13 17 views
18

मेरे पास एक चुनिंदा क्वेरी है जो संलग्न थंबनेल फ़ाइल वाली फ़ाइलों का चयन करती है और मुझे उन थंबनेल संलग्न करने की भी आवश्यकता होती है।MySQL चयन संघ?

मेरे वर्तमान एसक्यूएल क्वेरी

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 
संबंधित मुद्दे