这是我的样本数据结构和样本数据。在这里我要完成的工作是不显示具有“订户”类型的订户记录的客户。您将在数据集中看到Eli Manning有两个订阅记录。一个是“所有者”类型,另一个是“订户”类型。因此,他不应该出现在我的结果中,因为有一个“ SUBSCRIBER”记录实例。小奥德·贝克汉姆(Odell Beckham Jr.)仅拥有“所有者”类型的记录,因此他应该显示。
我尝试使用此查询,但是结果返回客户Saquan Barkley。您将看到该客户在“订户”表中有一个“订户”记录,因此我的sql无法正常工作。任何帮助将不胜感激。
我的查询
select distinct
a.customer_id,
a.fst_name,
a.last_name,
a.email,
b.subscription_type
from
customers a,
subscriptions b
where
a.customer_id <> (select customer_id from subscriptions
where subscription_type <> 'SUBSCRIBER')
AND b.subscription_type <> 'SUBSCRIBER'
order by customer_id asc;
表格和数据
DROP TABLE CUSTOMERS;
DROP TABLE SUBSCRIPTIONS;
CREATE TABLE "CUSTOMERS"
( "FST_NAME" VARCHAR2(50 BYTE),
"LAST_NAME" VARCHAR2(100 BYTE),
"CUSTOMER_ID" NUMBER NOT NULL ENABLE,
"EMAIL" VARCHAR2(150 BYTE),
CONSTRAINT "CUSTOMERS_PK" PRIMARY KEY ("CUSTOMER_ID"));
CREATE TABLE "SUBSCRIPTIONS"
( "ID" NUMBER NOT NULL ENABLE,
"CUSTOMER_ID" NUMBER NOT NULL ENABLE,
"SUBSCRIPTION_TYPE" VARCHAR2(20 BYTE),
"SERIAL_NUMBER" VARCHAR2(50 BYTE),
CONSTRAINT "SUBSCRIPTIONS_PK" PRIMARY KEY ("ID"));
INSERT INTO customers (fst_name, last_name, customer_id, EMAIL)
VALUES ('Eli', 'Manning', '1', 'emannning@giants.com');
INSERT INTO customers (fst_name, last_name, customer_id, EMAIL)
VALUES ('Odell', 'Beckham Jr.', '2', 'beckham@giants.com');
INSERT INTO customers (fst_name, last_name, customer_id, EMAIL)
VALUES ('Saquan', 'Barkley', '3', 'sbarkley@giants.com');
INSERT INTO customers (fst_name, last_name, customer_id, EMAIL)
VALUES ('Evan', 'Engram', '4', 'eEngram@giants.com');
INSERT INTO customers (fst_name, last_name, customer_id, EMAIL)
VALUES ('Nate', 'Solder', '5', 'nsolder@giants.com');
INSERT INTO customers (fst_name, last_name, customer_id, EMAIL)
VALUES ('Patrick', 'Omameh', '6', 'pomameh@giants.com');
INSERT INTO subscriptions (id, customer_id, subscription_type, serial_number)
VALUES ('1', '1', 'SUBSCRIBER', 'ASDF1234556');
INSERT INTO subscriptions (id, customer_id, subscription_type, serial_number)
VALUES ('2', '1', 'OWNER', 'ASDF1234556');
INSERT INTO subscriptions (id, customer_id, subscription_type, serial_number)
VALUES ('3', '2', 'OWNER', 'ASDF987657');
INSERT INTO subscriptions (id, customer_id, subscription_type, serial_number)
VALUES ('4', '3', 'SUBSCRIBER', 'ASDF11223344');
COMMIT;
答案 0 :(得分:1)
尝试以下代码:
SELECT a.customer_id, a.fst_name, a.last_name, a.email, b.subscription_type
FROM customers a
LEFT JOIN subscriptions b ON (b.customer_id = a.customer_id)
WHERE a.customer_id NOT IN (SELECT customer_id
FROM subscriptions
WHERE subscription_type = 'SUBSCRIBER')
ORDER BY a.customer_id ASC;
除了将代码更改为包括NOT IN
而不是<>
(然后将子查询条件更改为包括所有拥有的客户 >在它们的类型之间输入'SUBSCRIBER'
),我也将您的语法切换为使用显式JOIN
。此外,如果正确连接表,则不需要DISTINCT
。
答案 1 :(得分:1)
您所拥有的查询接近您想要的查询,而不是使用<>
试试not in
,子查询可能会返回多行,并且您无法对其应用<>
例如:
select distinct
a.customer_id,
a.fst_name,
a.last_name,
a.email,
b.subscription_type
from
customers a
inner join subscriptions b on a.customer_id = b.customer_id
where b.subscription_type <> 'SUBSCRIBER'
and a.customer_id not in
(
select customer_id from subscriptions where subscription_type = 'SUBSCRIBER'
)
order by customer_id asc;
答案 2 :(得分:0)
您的意思是这样的吗?
select distinct a.customer_id, a.fst_name, a.last_name, a.email,
b.subscription_type
from customers a, subscriptions b
where a.customer_id = b.customer_id
and a.customer_id not in (select customer_id from subscriptions
where subscription_type = 'SUBSCRIBER')
order by customer_id asc