Ir para conteúdo

POWERED BY:

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

Boris

Consulta a 2 Tabelas

Recommended Posts

Galera estou com um problema que creio que pra maioria de vcs seja facil ...

 

É o seguinte eu tenho um banco de dados com duas tabelas destintas.

 

TB_IMOVEL

TB_FINANCEIRA

 

porem pego dados para um response.write que foram gravada na tabela TB_IMOVEL porem o nome da Financeira nesta tabela esta apenas o codigo e na realidade quero mostrar o nome da financeira que correspondente ao ID que foi gravado na TB_IMOVEL.

 

Como faço pra pegar o ID gravado na TB_IMOVEL e no response.write mostrar ao inves do ID da TB_IMOVEL mostrar o nome da Financeira da TB_FINANCEIRA que corresponde ao ID da TB_IMOVEL.

 

Valew Galera estou colocando abaixo o codigo

 

<style type="text/css">

<!--

.style1 {color: #FFFFFF}

-->

</style>

<table width="100%" border="0" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF">

<tr>

<td><table width="98%" border="0" align="center" cellpadding="0" cellspacing="0" bgcolor="#F7F7F7">

<tr>

<td><table width="100%" border="0" cellpadding="0" cellspacing="0" bgcolor="#0000CC">

<tr>

<td width="20%" height="22" class="cx"><div align="center" class="style1 style47"><strong>Referência</strong></div></td>

<td width="22%" class="cx"><div align="center" class="style1 style47"><strong>Bairro</strong></div></td>

<td width="16%" class="cx"><div align="center" class="style1 style47"><strong>Dormitórios</strong></div></td>

<td width="17%" class="cx"><div align="center" class="style1 style47"><strong>Vagas</strong></div></td>

<td width="12%" class="cx"><div align="center" class="style47 style1"><strong>Financeira</strong></div></td>

<td width="13%" class="cx"><span class="style1 style47"><strong>Preço</strong></span></td>

</tr>

</table></td>

</tr>

<tr>

<td valign="top"><%

 

tp_imovel = Request.form("tp_imovel")

desc_aluguel = Request.form("desc_aluguel")

referencia = Request.form("referencia")

valor_minimo = Request.form("valor_minimo")

valor_maximo = Request.form("valor_maximo")

 

a=0

comandosql = "SELECT * FROM imovel "

clausula=""

if tp_imovel<>"0" then

if a=0 then

clausula = clausula & " where tp_imovel='"&tp_imovel&"'and ativo = '1' ORDER BY nivel_prioridade ASC"

a=a+1

else

clausula = clausula & " and tp_imovel='"&tp_imovel&"'"

end if

end if

 

if desc_aluguel <> "0" then

if a=0 then

clausula = clausula & " where desc_aluguel='"&desc_aluguel&"'"

a=a+1

else

clausula = clausula & " and desc_aluguel='"&desc_aluguel&"'"

end if

end if

 

if referencia <> "0" then

if a=0 then

clausula = clausula & " where referencia='"&referencia&"'"

a=a+1

else

clausula = clausula & " and referencia='"&referencia&"'"

end if

end if

 

 

if valor_minimo <> "" then

if a=0 then

clausula = clausula & " where valor>="&valor_minimo&" and valor<="&valor_maximo

a=a+1

else

clausula = clausula & " and valor>="&valor_minimo&" and valor<="&valor_maximo

end if

end if

 

comandosql = comandosql & clausula

 

if tp_imovel = "0" and desc_aluguel = "0" and referencia = "0" and valor_minimo = "0" and valor_maximo = "9999999"then

 

clausula = clausula & " where valor='0' and desc_alugel='0' and referencia='0' and valor_minimo='0' and valor_maximo='9999999'"

 

end if

 

'response.write comandosql:response.end

 

'############################################################################

#######################

Set conexao = Server.CreateObject("ADODB.Connection")

conexao.Open "DBQ=" & Server.MapPath("..\bd\bd.mdb") & ";Driver={Microsoft Access Driver (*.mdb)}"

Set consulta = conexao.Execute(comandosql)'############################################################################

#######################

 

total = 0

while not consulta.EOF

total = total + 1

consulta.MoveNext

wend

 

if total > 0 then

consulta.MoveFirst

 

 

%>

<table border="0" width="100%" cellspacing="1">

<%

 

while not consulta.EOF

 

n = consulta("id_imovel")

%>

<tr>

<td width="20%" height="20" bgcolor="#FFFFCC" class="style39"><div align="center" class="style36"><a href="ve_imovel.asp?id_imovel=<%=consulta("id_imovel")%>" style="text-decoration:none; color:#666666">

<% =consulta("referencia") %>

</a> </div></td>

<td width="22%" bgcolor="#FFFFCC" class="style39"><div align="center" class="style36"><a href="ve_imovel.asp?id_imovel=<%=consulta("id_imovel")%>" style="text-decoration:none; color:#666666">

<% =consulta("bairro") %>

</a></div></td>

<td width="16%" bgcolor="#FFFFCC" class="style39"><div align="center" class="style36">

<% =consulta("dormitorios") %>

</div></td>

<td width="17%" bgcolor="#FFFFCC" class="style39"><div align="center" class="style36">

<% =consulta("vagas") %>

</div></td>

<td width="12%" bgcolor="#FFFFCC" class="style39"><div align="right" class="style36">

<div align="center"><%=FormatCurrency(consulta("nome_fin")) %></div>

</div></td>

<td width="13%" bgcolor="#FFFFCC" class="style39"><%=FormatCurrency(consulta("valor")) %></td>

</tr>

<%

 

consulta.MoveNext

WEnd

 

%>

</table>

<%

' end if

end if

 

conexao.close

Set conexao = Nothing

 

%>

<%

'end if

' end if

%>

</td>

</tr>

</table></td>

</tr>

</table>

Valew http://forum.imasters.com.br/public/style_emoticons/default/thumbsup.gif

Compartilhar este post


Link para o post
Compartilhar em outros sites

poste a estrutura das tabelas e os campos relacionaveis

Compartilhar este post


Link para o post
Compartilhar em outros sites

você precisa fazer um inner join nas tabelas. quais campos são relacionados entre elas?

 

 

Bom eu tenho duas tabelas :

 

tb_financeira = id_financeira, n_financeira, ativo;

 

tb_imovel = id_imovel, titulo, n_imovel, valor, id_financeira, ativo ...

 

Na tb_imovel eu tenho id_financeira que foi cadastrado conforme a tb_financeira.

 

eu tenho que mostrar no response.write o nome da financeira porem na tb_imovel eu tenho é o id_financeira e não o n_financeira.

Compartilhar este post


Link para o post
Compartilhar em outros sites

SELECT CAMPOS FROM tb_financeira INNER JOIN tb_imovel ON tb_financeira.id_financeira = tb_imovel.id_financeira

 

em campos voce poe o que precisa

 

se campos tiverem o mesmo nome em amabas tabelas precisa mostrar qual tabela voce quer recuperar

 

tipo tb_imovel.id_financeira

Compartilhar este post


Link para o post
Compartilhar em outros sites

Ok obrigado Mario, eu acabei mudando um pouco a logica na alteração do item financeira.

 

Mas agradeço novamente pois eu tinha duvida como usar o inner join e foi esclarecido...

 

Obrigado

 

Valew Galera

Compartilhar este post


Link para o post
Compartilhar em outros sites

×

Informação importante

Ao usar o fórum, você concorda com nossos Termos e condições.