简体   繁体   中英

Two Table Join with Sub-query?

So I am working on an exercise to improve my SQL skills and I need to modify my query to add a subquery that looks for the existence of a customer ID in the CUSTOMERS table. The exercise also notes that this is a correlated sub-query so you will have to match the customer ID from the sub-query to the customer ID in the outer query. The title of the section of this exercise is "Two Table Join with Subquery". After researching and trying for a few hours now, I've exhausted all my resources but one. Any help would be much appreciated! (I am using Oracle Apex for this.)

SELECT ORDER_ID, ORDER_MODE, CUSTOMER_ID, PRODUCT_ID
FROM ORDERS
NATURAL JOIN ORDER_ITEMS;

客户表

ORDER_ITEMS表

订单表

PRODUCT_DESCRIPTIONS表

Using exists you can check for existence,

SELECT O.ORDER_ID, O.ORDER_MODE, O.CUSTOMER_ID, OI.PRODUCT_ID
FROM ORDERS O
INNER JOIN ORDER_ITEMS OI on O.ORDER_ID=OI.ORDER_ID
WHERE EXISTS(
SELECT * FROM CUSTOMERS C
WHERE O.CUSTOMER_ID=C.CUSTOMER_ID
);

This would do the second,

SELECT O.ORDER_ID, O.ORDER_MODE, O.CUSTOMER_ID, OI.PRODUCT_ID,P.TRANSLATED_NAME
FROM ORDERS O
INNER JOIN ORDER_ITEMS OI on O.ORDER_ID=OI.ORDER_ID
INNER JOIN PRODUCT_DESCRIPTIONS P on P.PRODUCT_ID=OI.PRODUCT_ID
WHERE EXISTS(
SELECT * FROM CUSTOMERS C
WHERE O.CUSTOMER_ID=C.CUSTOMER_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