简体   繁体   English

两表联接与子查询?

[英]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. 因此,我正在进行一项练习以提高SQL技能,并且需要修改查询以添加一个子查询,该子查询在CUSTOMERS表中查找客户ID的存在。 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. 练习还注意到,这是一个相关的子查询,因此您必须将子查询中的客户ID与外部查询中的客户ID进行匹配。 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.) (我为此使用Oracle Apex。)

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
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM