epic

Pro
Se incorporó
11 Febrero 2007
Mensajes
793
Hola a todos... tengo 2 tablas en la bd , ambas relacionadas por el rut.:

1.- pacientes
2.- seguimiento

En la tabla "pacientes" tengo 1 registro con el Rut "11111111-1" y en la tabla "seguimiento" tengo 2 registros con el mismo Rut "11111111-1" (ya que un paciente puede tener mas de un seguimiento) y si hago la consulta:

SELECT * FROM pacientes, seguimiento WHERE pacientes.rut=seguimiento.rut

Me trae datos duplicados del paciente "11111111-1" ya que esta 2 veces en la tabla "seguimiento", lo que quiero hacer es que me lo traiga solo una vez.


En PHP tengo:
$sql2 = "SELECT * FROM pacientes, seguimiento WHERE pacientes.rut=seguimiento.rut";
$resultado2 = $mysqli->query($sql2) or trigger_error($mysqli->error);

y mas abajo una tabla:
<div class="row table-responsive">
<table class="table table-striped">
<thead>
<tr>
<th>Rut</th>
<th>Nombres</th>
<th>Apellidos</th>
<th>Email</th>
<th>Telefono</th>
<th>Tipo Diabetes</th>
<th>Fecha Ingreso</th>
<th>Fecha Educación</th>
<th></th>
<th></th>
</tr>
</thead>

<tbody>
<?php while($row2= $resultado2->fetch_array(MYSQLI_ASSOC)) { ?>
<tr>
<td><?php echo $row2['rut']; ?></td>
<td><?php echo $row2['nombres']; ?></td>
<td><?php echo $row2['apellidos']; ?></td>
<td><?php echo $row2['email1']; ?></td>
<td><?php echo $row2['telefono1']; ?></td>
<td><?php echo $row2['tipo_diabetes']; ?></td>
<td><?php echo $row2['f_ing_prog']; ?></td>
<td><?php echo $row2['f_educacion']; ?></td>
<td><a href="modificar.php?rut=<?php echo $row2['rut']; ?>"><span class="glyphicon glyphicon-pencil" title="Editar Registro"></span></a></td>
<td><a href="seguimiento.php?rut=<?php echo $row2['rut']; ?>"><span class="glyphicon glyphicon-plus" title="Agregar Seguimiento"></span></a></td>
<td><a href="#" data-href="eliminar.php?rut=<?php echo $row2['rut']; ?>" data-toggle="modal" data-target="#confirm-delete" title="Eliminar Registro"><span class="glyphicon glyphicon-trash"></span></a></td>
</tr>
<?php } ?>
</tbody>
</table>
</div>



El Campo "Fecha Educación (f_educacion)" pertenece a la tabla "seguimiento", y todos los demas a la table "pacientes" de la bd.


Si me puede echar una manito... gracias!
 

el_dva

Capo
Se incorporó
23 Noviembre 2009
Mensajes
203
Hola otra vez @el_dva ... queria editar esa consulta para agregar otro JOIN con otro SELECT y consultar otra tabla:

PHP:
<?php
    require 'conexion.php';
    $campo= "";
    if(!empty($_POST))
    {
    $valor = $_POST['rut'];
    if(!empty($valor)){
    $campo= "WHERE pacientes.rut LIKE '%$valor'";
    }
}
$sql = "SELECT pacientes.rut, pacientes.nombres, pacientes.apellidos, pacientes.email1, pacientes.telefono1, pacientes.tipo_diabetes, pacientes.f_ing_prog, educacion.f_educacion, seguimiento.num_seguimiento, seguimiento.alta_center
        FROM pacientes
        JOIN (SELECT rut, MAX(f_educacion) AS f_educacion FROM educacion GROUP BY rut) AS educacion ON pacientes.rut = educacion.rut
        JOIN (SELECT rut, MAX(num_seguimiento) AS num_seguimiento, alta_center FROM seguimiento GROUP BY rut) AS seguimiento ON pacientes.rut = seguimiento.rut
        $campo";
    $resultado = $mysqli->query($sql) or trigger_error($mysqli->error);
?>

El Primer JOIN que pusiste lo cambie un poco ya que cambio el nombre de esa tabla a "educación" y se creo otra con el nombre "seguimiento" (la "seguimiento" anterior ahora se llama "educación" y el actual seguimiento tiene otros campos).

Agregue la linea:

PHP:
JOIN (SELECT rut, MAX(num_seguimiento) AS num_seguimiento, alta_center FROM seguimiento GROUP BY rut) AS seguimiento ON pacientes.rut = seguimiento.rut

y funciona bien hasta cierto punto... como el "seguimiento" se agrega después que el paciente, al principio esa tabla (seguimiento) esta vacía y por lo tanto no hay un "Rut" y la vinculación entre ambas tablas no funciona (pacientes.rut = seguimiento.rut), por lo tanto cuando se hace la consulta no me muestra nada a no ser que le meta a mano en la BD un seguimiento al paciente... si hago eso me muestra todos los campos (rut, nombre, apellidos, mail, telefono, etc), inclusive los de seguimiento.


:S

@epic Solo debes usar un LEFT JOIN en vez de FULL JOIN, allí te mostrará aunque seguimiento este vacía, te tiene que quedar así:

PHP:
PHP:
<?php
    require 'conexion.php';
    $campo= "";
    if(!empty($_POST))
    {
    $valor = $_POST['rut'];
    if(!empty($valor)){
    $campo= "WHERE pacientes.rut LIKE '%$valor'";
    }
}
$sql = "SELECT pacientes.rut, pacientes.nombres, pacientes.apellidos, pacientes.email1, pacientes.telefono1, pacientes.tipo_diabetes, pacientes.f_ing_prog, educacion.f_educacion, seguimiento.num_seguimiento, seguimiento.alta_center
        FROM pacientes
        JOIN (SELECT rut, MAX(f_educacion) AS f_educacion FROM educacion GROUP BY rut) AS educacion ON pacientes.rut = educacion.rut
        LEFT JOIN (SELECT rut, MAX(num_seguimiento) AS num_seguimiento, alta_center FROM seguimiento GROUP BY rut) AS seguimiento ON pacientes.rut = seguimiento.rut
        $campo";
    $resultado = $mysqli->query($sql) or trigger_error($mysqli->error);
?>
 
Upvote 0

epic

Pro
Se incorporó
11 Febrero 2007
Mensajes
793
@epic Solo debes usar un LEFT JOIN en vez de FULL JOIN, allí te mostrará aunque seguimiento este vacía, te tiene que quedar así:

PHP:
PHP:
<?php
    require 'conexion.php';
    $campo= "";
    if(!empty($_POST))
    {
    $valor = $_POST['rut'];
    if(!empty($valor)){
    $campo= "WHERE pacientes.rut LIKE '%$valor'";
    }
}
$sql = "SELECT pacientes.rut, pacientes.nombres, pacientes.apellidos, pacientes.email1, pacientes.telefono1, pacientes.tipo_diabetes, pacientes.f_ing_prog, educacion.f_educacion, seguimiento.num_seguimiento, seguimiento.alta_center
        FROM pacientes
        JOIN (SELECT rut, MAX(f_educacion) AS f_educacion FROM educacion GROUP BY rut) AS educacion ON pacientes.rut = educacion.rut
        LEFT JOIN (SELECT rut, MAX(num_seguimiento) AS num_seguimiento, alta_center FROM seguimiento GROUP BY rut) AS seguimiento ON pacientes.rut = seguimiento.rut
        $campo";
    $resultado = $mysqli->query($sql) or trigger_error($mysqli->error);
?>
Weeeena! te pasaste... gracias!
 
Upvote 0
Subir