Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySql SELECT union for different columns? [duplicate]

Tags:

mysql

I have a select query that selects files with a thumbnail file attached and I also need to get those with no thumbnail attached.

My current sql query is

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 

I need to also get

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 

I would normally just do a

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

But the problem is the second one has a different set of columns (minus the files.* part)

I cannot for the life of me figure out how to do this.

like image 570
Steven Avatar asked Sep 13 '11 20:09

Steven


People also ask

Does UNION remove duplicates in MySQL?

Description. The MySQL UNION operator is used to combine the result sets of 2 or more SELECT statements. It removes duplicate rows between the various SELECT statements. Each SELECT statement within the UNION operator must have the same number of fields in the result sets with similar data types.

Can you UNION with different columns?

Using UNION on Multiple FieldsWe can apply UNION on multiple columns and can also order the results using the ORDER BY operator in the end. This will result in the following: The result is sorted according to the “Dept_ID.” We can also filter the rows being retrieved by each SELECT statement.

Does UNION allow for duplicates?

UNION: only keeps unique records. UNION ALL: keeps all records, including duplicates.

How do you avoid duplicates in UNION query?

The SQL UNION ALL operator does not remove duplicates. If you wish to remove duplicates, try using the UNION operator.


2 Answers

If you have different fields that have different meaning too, you can't and shouldn't return them in the same position. You can however 'fill in the blanks' by adding null to your fields, like this:

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  

You can provide an alias for the null in the first select. You can add aliases in the second select for clarity, but it won't be used. You can even use constant values which you can use to distinguish the record type later.

like image 125
GolezTrol Avatar answered Sep 21 '22 10:09

GolezTrol


If select A does not have a column, simply use a null value

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 

just need to be sure that each column you are matching are of the same data type

like image 21
dispake Avatar answered Sep 22 '22 10:09

dispake