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

Asked

Viewed 88 times

1

I wonder 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, there is a way to perform these 2 queries as a single, at once, 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());

1 answer

0


You can use the instruction UNION, but it needs the number and types of the resulting columns to be equal in both queries.

Ex:

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

Column names need not be equal.


The documentation of Mysql explains the use of UNION in detail.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.