AND query, OR in MYQL

0

How about, good afternoon. I have a database with 4 tables that are linearly related. To which I enter data that depend on different precedents. I have the following diagram which I enter into the base.

In it you can see that the letter "I" is only for "E of J". However, in my query it shows me that "I" has all the other activities.

This is the code I use to make the query.

SELECT PAPA.idPAPA PAPA,HIJO.idHIJO HIJO,NIETO.idNIETO 
NIETO,BISNIETO.idBISNIETO BISNIETO
FROM PAPA,HIJO,NIETO,BISNIETO
WHERE HIJO.PreH=idPAPA
AND NIETO.PreN=HIJO.idHIJO 
AND (BISNIETO.PreB=NIETO.idNIETO OR NIETO.PreN=HIJO.idHIJO)
;

I would like to know how I can exclude the "I" from the other activities in my query. As follows:

    
asked by Gael Rodriguez 15.03.2018 в 22:28
source

1 answer

1

The problem is the way you are relating the query, the ideal would be to perform the query as follows:

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;

I hope it will be useful, if the answer solves your problem, do not forget to rate.

    
answered by 15.03.2018 в 22:42