简体   繁体   中英

Combine two queries in PostgreSQL

I need help with the union in a single array of two queries :

In a query related absences achievement of students in a quarter :

$sSqlAsistencia =
         " SELECT
                ca.idcadete,                    
                coalesce(sum(i.cantidad),0) as cantidad

            FROM 
                cadetes ca,
                cursos c,
                cursos_cadetes cc
                left join inasistencias i on i.fk_idcurso = cc.fk_idcurso and i.fk_idcadete = cc.fk_idcadete
            WHERE 
                c.habilitado = true
                and ca.habilitado = true
                and c.fk_idanolectivo = ".$aAnoLectivoBuscar."
                and c.fk_idano = ".$aAnoBuscar."
                and c.fk_iddivision = ".$aDivisionBuscar."
                and cc.fk_idcurso = c.idcurso
                and cc.fk_idcadete = ca.idcadete
                and (EXTRACT(MONTH FROM i.fecha)  in ".$trimestre ." or i.cantidad is null)

            GROUP BY
                ca.idcadete

            ";
    $sSqlInasistencia = $oDB->Query($sSqlAsistencia);

idcadete | cantidad

203      |    4
305      |    0
120      |    10 

Then a piece of code I have as a result a student / cadet award for meeting other inquiries:

$sSql = " SELECT idcadete, nombre, apellido, matricula
         FROM cadetes
        WHERE idcadete in
        (" . $sSqlPromedioEnCadaMateria . " INTERSECT " . $sSqlPromedioConducta;
        if (strlen($aPromedioGeneralEdFisicaMayorABuscar)) {
          $sSql .= " INTERSECT " . $sSqlPromedioEnEdFisica;
        }


$sSql .=    ")";
    $rsCadetesConPremio=$oDB->query($sSql);

idcadete | nombre | apellido | matricula
203      | adrian | perez    | 212121

Try to link the two consultations with INNER JOIN but shoot me wrong , as it should manage to combine both queries like this:

$premio = " SELECT a.idcadete, a.nombre, a.apellido, a.matricula, b.cantidad
        FROM ".$rsCadetesConPremio."a inner join ".$sSqlInasistencia."b on a.idcadete = b.idcadete
        ORDER BY a.idcadete";

----ERROR-------

I need the following result: $premio

idcadete | nombre | apellido | matricula| cantidad
203      | adrian | perez    | 212121   |     4

Yes, you should be able to join any two arbitrary queries by wrapping the results of both as derived tables. You will however need to enclose both derived tables in parenthesis : FROM ( ...) a INNER JOIN ( ...) b

ie:

"SELECT a.idcadete, a.nombre, a.apellido, a.matricula, b.cantidad
        FROM (".$rsCadetesConPremio.") a inner join (".$sSqlInasistencia.") b on a.idcadete = b.idcadete
        ORDER BY a.idcadete"

Here's a simplified Fiddle .

 $sSqlAsistencia = " SELECT ca.idcadete, ca.nombre, ca.apellido, ca.matricula coalesce(sum(i.cantidad),0) as cantidad FROM cadetes ca, cursos c, cursos_cadetes cc left join inasistencias i on i.fk_idcurso = cc.fk_idcurso and i.fk_idcadete = cc.fk_idcadete WHERE c.habilitado = true and ca.habilitado = true and c.fk_idanolectivo = ".$aAnoLectivoBuscar." and c.fk_idano = ".$aAnoBuscar." and c.fk_iddivision = ".$aDivisionBuscar." and cc.fk_idcurso = c.idcurso and cc.fk_idcadete = ca.idcadete and (EXTRACT(MONTH FROM i.fecha) in ".$trimestre ." or i.cantidad is null) and ca.idcadete in (" . $sSqlPromedioEnCadaMateria . " INTERSECT " . $sSqlPromedioConducta; if (strlen($aPromedioGeneralEdFisicaMayorABuscar)) { $sSqlAsistencia .= " INTERSECT " . $sSqlPromedioEnEdFisica; } $sSqlAsistencia .= ") GROUP BY ca.idcadete "; 

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