Подтвердить что ты не робот

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

Мне нужно также получить

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)

Но проблема в том, что вторая имеет другой набор столбцов (минус файлы. * part)

Я не могу для жизни понять, как это сделать.

4b9b3361

Ответ 1

Если у вас разные поля, имеющие другой смысл, вы не можете и не должны возвращать их в том же положении. Однако вы можете заполнить пробелы, добавив 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 

Вы можете предоставить псевдоним для null в первом выборе. Вы можете добавить псевдонимы во второй выбор для ясности, но он не будет использоваться. Вы даже можете использовать постоянные значения, которые вы можете использовать, чтобы впоследствии различать тип записи.

Ответ 2

Если в элементе A отсутствует столбец, просто используйте нулевое значение

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

просто нужно быть уверенным, что каждый столбец, который вы сопоставляете, имеет один и тот же тип данных

Ответ 3

Вы можете подделать столбец для объединения.

например.

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