Jump to content


Photo

Ordem De Datas Em Select De 2 Tabelas


  • Faça o login para participar
Nenhuma resposta neste tópico

#1 Lusigmas

Lusigmas

    Novato no fórum

  • Usuários
  • 17 posts
  • Sexo:Feminino
  • Localidade:São Paulo / SP

Posted 27/06/2010, 11:51

Bom dia! Após dias quebrando a cabeça mais uma vez venho buscar seu apoio.
Desenvolvo um sistema de gestão da área comercial com um relatório que mostra os dados de duas tabelas.
- tabela cadastro: dados do cliente.
- tabela histórico: registro de todos os contatos com o cliente.

Preciso de um relatório que mostre dados do cadastro e o registro mais recente do histórico:
------------------
Cliente: ABC, etc, etc
Histórico: 25/06/2010, proposta entregue.
------------------

Estudando as soluções do fórum montei o relatório e funcionou, porém ele só mostra o primeiro registro da tabela histórico e não o mais recente.
Já tentei de tudo (order, group, max, iframe etc) mas penso que não consegui aplicar os comandos da maneira certa.
Peço ajuda, se possível com um exemplo específico.

Segue o código:
----------------------
<?php
include "validar_session.php";
include "Config/config_sistema.php";
$consulta = mysql_query("select cadastro.*, historico.data_historico, historico.historico from cadastro
LEFT JOIN historico ON cadastro.id = historico.id_cadastro
WHERE cadastro.login='$login_usuario' and cadastro.categoria = '2. em andamento'
GROUP BY cadastro.id
ORDER by cadastro.razaosocial");

?>

<html>
<head>
<title>Documento sem t&iacute;tulo</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="../gen_estilo.css" rel="stylesheet" type="text/css">
</head>

<body>
<table width="880" border="0" cellspacing="0" cellpadding="0">
<tr bgcolor="#F9E6F9">
<?php
while($linhas = mysql_fetch_object($consulta)) {
?>
<td height="10" colspan="10" class="gentextocenterescuro"></td>
</tr>
<tr>
<td height="15" colspan="2" class="gentextoform">Raz&atilde;o Social::</td>
<td height="26" colspan="3" class="gentextoformblack"><strong><?php echo $linhas->razaosocial;?></strong>
<div align="right"></div></td>
<td width="93" class="gentextoform">N&ordm; vidas</td>
<td width="104" height="26" class="gentextoformblue"><?php echo $linhas->funcnr;?></td>
<td width="48" height="26" class="gentextoformblacknegrito"> <div align="right"><span class="gentextoform">C&oacute;d.:</span>
</div></td>
<td width="88" height="26" class="gentextoformblacknegrito"><?php echo $linhas->id;?>
<div align="center"></div></td>
</tr>
<tr bgcolor="#F5F5F5">
<td height="15" colspan="2" class="gentextoform">Situa&ccedil;&atilde;o:</td>
<td height="26" colspan="2" class="gentextoformblue"><?php echo $linhas->situacao;?></td>
<td height="26" colspan="2" class="gentextoform"> <div align="right">Data
do cadastro:</div></td>
<td width="104" height="26" class="gentextoformblue"><?php echo date("d/m/Y", strtotime($linhas->data1));?></td>
<td height="26" class="gentextoform"> <div align="right">visita:</div></td>
<td height="26" class="gentextoformblue"><?php echo date("d/m/Y", strtotime($linhas->data4));?></td>
</tr>
<tr>
<td height="18" colspan="2" class="gentextoform">Contato:</td>
<td width="235" height="26" class="gentextoformblue"><?php echo $linhas->nome1;?></td>
<td width="82" height="26" class="gentextoformblue"> <div align="right"></div>
<?php echo $linhas->ddd1;?></td>
<td height="26" colspan="2" class="gentextoformblue"><?php echo $linhas->fone1;?>
</td>
<td height="26" class="gentextoformblue"> <div align="center"><?php echo $linhas->fone3;?></div></td>
<td height="26" class="gentextoformblue">&nbsp;</td>
<td height="26" class="gentextoformblue">&nbsp; </td>
</tr>
<tr bgcolor="#F5F5F5">
<td height="24" colspan="2" class="gentextoform">Obs:</td>
<td height="26" colspan="7" class="gentextoformblue"><?php echo $linhas->obs;?></td>
</tr>
<tr>
<td width="61" height="24" bgcolor="#FEFCFE" class="gentextoform">Hist&oacute;rico:</td>
<td width="79" bgcolor="#FEFCFE" class="gentextoformblack"><?php echo date("d/m/Y", strtotime($linhas->data_historico));?></td>
<td colspan="7" bgcolor="#FEFCFE" class="gentextoformblue"> <?php echo $linhas->historico;?>
</td>
</tr>
<tr bgcolor="#F9E6F9">
<td height="10" colspan="2" class="gentextoform"></td>
<td height="10" colspan="7" class="gentextoformblue"></td>
</tr>
<?php
}
?>
</table>
</body>
</html>
-------------------------------------
Desde já agradeço se puderem ajudar.
Um abraço.

=============================================================================================
[Resolvido].
O colega Felipe Pena me ajudou. Deixarei a solução aqui para quem precisar:
------------

$consulta_base = "SELECT cadastro.*,
h.data_historico,
h.historico
FROM cadastro
LEFT JOIN (
SELECT hh.data_historico,
hh.historico,
hh.id_cadastro
FROM historico hh, (
SELECT MAX(data_historico) max_dt, id_cadastro
FROM historico
GROUP BY id_cadastro
) hist_max
WHERE hh.data_historico = hist_max.max_dt
AND hh.id_cadastro = hist_max.id_cadastro
GROUP BY hh.id_cadastro
) h
ON cadastro.id = h.id_cadastro
WHERE cadastro.login='$login_usuario' and cadastro.categoria = '1. pré cadastro'
GROUP BY cadastro.id
ORDER by cadastro.razaosocial";
$consulta1 = mysql_query($consulta_base) or die(mysql_error());
---------------

Edição feita por: Lusigmas, 27/06/2010, 11:53.





1 user(s) are reading this topic

0 membro(s), 1 visitante(s) e 0 membros anônimo(s)

IPB Skin By Virteq