Add values from an Inner Join to 2 variables

Asked

Viewed 344 times

1

I wonder if you have any way to get the results of a separate select through php. I have two foreign keys in the same table, setting for the same place, they are Tj.fk_time_mandante and Tj.fk_time_visitante.

SELECT tt.ds_nome, tti.ds_nome
FROM tb_jogo AS tj INNER JOIN tb_time AS tt ON tj.fk_time_mandante = tt.id_time
INNER JOIN tb_time AS tti ON tj.fk_time_visitante = tti.id_time

I already used the mysql_fetch_array but it joins the tt.ds_name and Tti.ds_name, I wanted them to be inserted into separate variables Example:

while($row = mysql_fetch_array($sql)){
 echo $row['ds_nome1']; <- para tt.ds_time da consulta
 echo $row['ds_nome2']; <- para tti.ds_time da consulta
}

I already tested the query in the database, and returned the correct result. Note: I am not using any framework

1 answer

3


Give an alias (nickname) to your columns using AS. This is not PHP, but standard SQL. See more: http://www.w3schools.com/sql/sql_alias.asp

SELECT tt.ds_nome AS ds_nome1, tti.ds_nome AS ds_nome2
FROM tb_jogo AS tj INNER JOIN tb_time AS tt ON tj.fk_time_mandante = tt.id_time
INNER JOIN tb_time AS tti ON tj.fk_time_visitante = tti.id_time

Browser other questions tagged

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