如何在 SQL 子查询中使用 WHERE EXISTS?

How to use WHERE EXISTS in a SQL subquery?

我需要找到在 VICTORIA AVE 的一家酒吧点过酒的不同饮酒者。 2020 年 3 月。

以下是我试图从中检索数据的表:

CREATE TABLE ORDERS(    /* Drinkers visit pubs and consumes drinks */
DRINKER     VARCHAR(30) NOT NULL,   /* Drinker name */
PUB         VARCHAR(30) NOT NULL,   /* Pub name */
ODATE       DATE        NOT NULL,   /* Order date   */
DRINK       VARCHAR(30) NOT NULL,   /* Drink name   */
DRINK_NO    DECIMAL(2)  NOT NULL,   /* A sequence number of a drink */
    CONSTRAINT ORDERS_PKEY PRIMARY KEY(DRINKER, PUB, ODATE, DRINK, DRINK_NO),
    CONSTRAINT ORDERS_FKEY1 FOREIGN KEY(PUB, DRINK) REFERENCES SERVES(PUB, DRINK),
    CONSTRAINT ORDERS_FKEY2 FOREIGN KEY(DRINKER) REFERENCES DRINKERS(DRINKER)   );

CREATE TABLE LOCATED(   /* Pubs have locations */
PUB         VARCHAR(30) NOT NULL,   /* Pub name */
STREET      VARCHAR(30) NOT NULL,   /* Street name  */
BLDG_NO     DECIMAL(4)  NOT NULL,   /* Building number  */
    CONSTRAINT LOCATED_PKEY PRIMARY KEY(PUB) );

这是我目前的 SELECT 声明:

SELECT DISTINCT DRINKER 
FROM ORDERS 
WHERE EXISTS (SELECT PUB 
              FROM LOCATED
              WHERE STREET = 'VICTORIA AVE.' 
              AND ORDERS.ODATE >= STR_TO_DATE('01-MAR-2020','%d-%M-%Y')
              AND ORDERS.ODATE <= STR_TO_DATE('01-MAR-2020','%d-%M-%Y'));

子查询需要在其 WHERE 子句中与主查询相关联。

SELECT DISTINCT DRINKER 
FROM ORDERS 
WHERE EXISTS (SELECT PUB 
              FROM LOCATED
              WHERE LOCATED.PUB = ORDERS.PUB
              AND STREET = 'VICTORIA AVE.' 
              AND ORDERS.ODATE >= STR_TO_DATE('01-MAR-2020','%d-%M-%Y')
              AND ORDERS.ODATE <= STR_TO_DATE('31-MAR-2020','%d-%M-%Y'));