MySql SELECT union for different columns? [duplicate]

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.

Leave a Comment