MySQL subConsulta

0

How about, I have a query which works well.

SELECT PAPA.idPAPA AS PAPA, HIJO.idHIJO AS HIJO, NIETO.idNIETO AS NIETO,
BISNIETO.idBISNIETO AS BISNIETO
FROM PAPA 
LEFT JOIN HIJO ON HIJO.PreH=PAPA.idPAPA 
LEFT JOIN NIETO ON NIETO.PreN=HIJO.idHIJO 
LEFT JOIN BISNIETO ON BISNIETO.PreB=NIETO.idNIETO 
ORDER BY idPAPA,idHIJO,idNIETO,idBISNIETO ASC;

I returned the following table, when I entered 2 different PAPÁS:

Which is correct, however, I want to add a WHERE idPAPA="A", so that only the PAPA query "A".

Like this:

I have this query but it does not run.

SELECT idPAPA, idHIJO, idNIETO, idBISNIETO
FROM
(SELECT PAPA.idPAPA AS PAPA, HIJO.idHIJO AS HIJO,NIETO.idNIETO AS
NIETO,BISNIETO.idBISNIETO AS BISNIETO 
FROM PAPA 
LEFT JOIN HIJO ON HIJO.PreH=PAPA.idPAPA 
LEFT JOIN NIETO ON NIETO.PreN=HIJO.idHIJO 
LEFT JOIN BISNIETO ON BISNIETO.PreB=NIETO.idNIETO 
WHERE idPAPA="A"
) ORDER BY idPAPA,idHIJO,idNIETO,idBISNIETO ASC
;

Thank you.

    
asked by Gael Rodriguez 25.05.2018 в 00:58
source

1 answer

1

The tables within the query must have a name, try this:

SELECT *
FROM
(SELECT PAPA.idPAPA AS PAPA, HIJO.idHIJO AS HIJO,NIETO.idNIETO AS
NIETO,BISNIETO.idBISNIETO AS BISNIETO 
FROM PAPA 
LEFT JOIN HIJO ON HIJO.PreH=PAPA.idPAPA 
LEFT JOIN NIETO ON NIETO.PreN=HIJO.idHIJO 
LEFT JOIN BISNIETO ON BISNIETO.PreB=NIETO.idNIETO 
WHERE idPAPA="A"
ORDER BY PAPA.idPAPA,HIJO.idHIJO,NIETOidNIETO,BISNIETO.idBISNIETO ASC
) as a 
;
    
answered by 25.05.2018 / 01:09
source