简体   繁体   中英

Oracle query using 3 tables, sql

I have a problem with a query for Oracle with this scenario:

Table People 
ID | Name 
1 | juan 
2 | pedro 
3 | luis 

Table Properties 
ID | nombre_inmueble | FK to Table People 
1 | house | 1 
2 | garden | 1 
3 | terrace | 1 
4 | moto | 2
5 | jet  | 2 

Table Accessories 
ID | accessories | FK Table Properties
1 | windows | 1 
2 | doors | 1 
3 | scale | 2 
4 | plants | 3 
5 | motor | 4

What I want is only the people who have Properties and that have ALL Accessories, in this case the output would be

1 | juan 

What would be the query?

Your query will look like this:

SELECT *
FROM People P
WHERE EXISTS(
        SELECT 1
        FROM Properties T
        WHERE T.PEOPLE= P.ID
)
AND NOT EXISTS(
        SELECT 1
        FROM Properties T
        WHERE T.PEOPLE= P.ID
        AND NOT EXISTS(
                SELECT 1
                FROM Accessories A
                WHERE A.Properties = T.ID
        )
);

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