How to do
You can do this using the UNION
function, thus joining the results of the 2 tables, and using subquery :
SELECT * FROM (
(SELECT nome as nomefinal, sobrenome, idade FROM primeiratabela)
UNION ALL
(SELECT name as nomefinal, last_name as sobrenome, age as idade FROM segundatabela)
) tabX
ORDER BY nomefinal
Complementing
In UNION
, you will have to have the same fields in querys , even though it is NULL
.
Example: in a table I have the fieldX and I do not have the fieldY. Already in tableB, I do not have the X field and I have the Y field.
I can merge into different columns:
SELECT * FROM (
(SELECT campoX, NULL FROM tabelaA)
UNION ALL
(SELECT NULL, campoY FROM tabelaB)
) tab_aux
Or in the same column, as I did in your case, using alias :
SELECT * FROM (
(SELECT campoX as campoU FROM tabelaA)
UNION ALL
(SELECT campoY as campoU FROM tabelaB)
) tab_aux
More about the function UNION .