Is there a way to join 2 queries of distinct tables in a single query?

1

I would like to know if there is a way to join 2 queries of different tables in a single query?

For example, in the code below I have 2 queries in separate tables. However, is there a way to perform these 2 queries as a single, one at a time, instead of having to do 2 variables and two separate queries?

$query1 = mysql_query("SELECT cod_empresa, razao_social FROM tbl_empresa ORDER by razao_social ASC") or die(mysql_error());
$query2 = mysql_query("SELECT cod_tipo, tipo_publicacao FROM tbl_tipo_publicacao by tipo_publicacao ASC") or die(mysql_error());
    
asked by anonymous 23.02.2017 / 15:20

1 answer

0

You can use the UNION statement, but you need the number and types of the resulting columns to be the same in both queries.

Ex:

SELECT id, nome FROM alunos
UNION
SELECT id, nome FROM professores

Column names do not have to be the same.

The MySQL documentation explains the use of UNION in details.     

23.02.2017 / 15:24