2011-09-13 44 views
18

Tôi có một truy vấn chọn để chọn tệp có tệp hình thu nhỏ được đính kèm và tôi cũng cần có những tệp không có hình thu nhỏ đính kèm.Liên kết MySql SELECT cho các cột khác nhau?

truy vấn sql hiện tại của tôi là

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 

tôi cũng cần phải có được

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 

tôi sẽ thường chỉ làm một

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

Nhưng vấn đề là một trong những thứ hai có tập hợp các cột khác nhau (trừ các tệp. * một phần)

Tôi không thể cho cuộc sống của tôi tìm ra cách để làm điều này.

+4

Bạn biết bạn được phép sử dụng ngắt dòng, không? Và bí danh cũng có thể giúp ích. :) – GolezTrol

Trả lời

38

Nếu bạn có các trường khác nhau có ý nghĩa khác nhau, bạn không thể và không nên trả lại chúng ở cùng một vị trí. Tuy nhiên, bạn có thể 'điền vào chỗ trống' bằng cách thêm null vào các trường của bạn, như sau:

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 

Bạn có thể cung cấp bí danh cho giá trị rỗng trong lần chọn đầu tiên. Bạn có thể thêm bí danh trong lựa chọn thứ hai để làm rõ, nhưng nó sẽ không được sử dụng. Bạn thậm chí có thể sử dụng các giá trị không đổi mà bạn có thể sử dụng để phân biệt loại bản ghi sau này.

10

Nếu chọn A không có một cột, chỉ cần sử dụng một giá trị null

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 

chỉ cần phải chắc chắn rằng mỗi cột bạn đang so khớp là các kiểu dữ liệu cùng

+0

Cảm ơn bạn đã chỉ ra phương pháp này. Tôi có một số cột bằng nhau để truy vấn cho mỗi bảng, nhưng một cột trong mỗi cột có các kiểu dữ liệu khác nhau. Các null đã giúp sắp xếp điều này và nhận được kết quả tôi cần. Cảm ơn! –

5

Bạn có thể giả một cột để làm công đoàn

ví dụ:

select x, y from A 
    union 
    select z, null from B 
Các vấn đề liên quan