简体   繁体   中英

php mysql_query related queries

I'm trying to run a query to a table in my database and the id field of the row that returns me to run a query to get the assistance field of a different table.

The code I'm trying to do is for a webService, my idea is to brand all fields of the first consultation separated by ; but the assistance of the second table, and separate each row returned by field :

I just started programming in php, so I apologize if I did not express myself well or ask very basic things.

Thanks for your time and help.

CODE

if($tabla =="servicio")
    {
        $consulta = "SELECT `tipo` , `nombre` , `descripcion` , `hora` , `minuto` ,`lugar` , `duracion`,`fecha` , `horacero`, `id` FROM `".$tabla;

        $resultado= mysql_query($consulta,$enlace);


        $arraySalida = array();
        while($registro = mysql_fetch_assoc ($resultado) ):

             $consulta2 = "SELECT `asistencia` FROM `voluntarioServicio` WHERE id = `".$registro['id']."` AND emei = '".$emei."' ";
             $resultado2= mysql_query($consulta,$enlace);
                        $registro2 = mysql_fetch_assoc ($resultado2);
             $cadena = "{$registro['tipo']};{$registro['nombre']};{$registro['descripcion']};{$registro['hora']};{$registro['minuto']};{$registro['lugar']};{$registro['duracion']};{$registro['fecha']};{$registro['horacero']};{$registro2['asistencia']}";
             $arraySalida[]= $cadena;

        endwhile;

    echo implode(":",$arraySalida); 
    }

Thanks, I have already solved. I leave here the code, I hope someone will serve.

CODE

        $consulta = "SELECT tipo,nombre,descripcion,hora,minuto,lugar,duracion,fecha,horacero,id,asistencia FROM ( SELECT ".$tabla.".*, voluntarioServicio.asistencia  FROM ".$tabla."left join voluntarioServicio on (".$tabla.".id=voluntarioServicio.id) ) as tabla";
            $resultado= mysql_query($consulta,$enlace);
            $arraySalida = array();
            while($registro = mysql_fetch_assoc ($resultado) ):

                     $cadena = "{$registro['tipo']};{$registro['nombre']};{$registro['descripcion']};{$registro['hora']};{$registro['minuto']};{$registro['lugar']};{$registro['duracion']};{$registro['fecha']};{$registro['horacero']};{$registro['asistencia']}";
                     $arraySalida[]= $cadena;
            endwhile;

            echo implode(":",$arraySalida);

better SQL is

$consulta = "SELECT `table_a`.*, `table_b`.`asistencia`
    FROM `".$tabla."` `table_a`
        JOIN `voluntarioServicio` `table_b` ON `table_a`.`id` = `table_b`.`id`;";

because you get everything from inner select

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM