列出僱員姓名(Ename),他們同時擁有49008個郵政編碼客戶和49009個郵政編碼客戶。MySQL根據同一列中的兩個不同值找到結果
我很努力回答基於上述表格的上述查詢。
如果表之間的名稱匹配假定約束。
我可以通過左連接和羣組輕鬆地過濾到名稱和zip,但之後掙扎,我不知道適當的地方或有聲明。我假設它可以通過子查詢做得更好,但不能確定。理想情況下,一個查詢。
請謝謝。
1)創建並插入例如數據的語句:
Create table Employees (EM_Eno INT NOT NULL, EM_Ename VARCHAR(50), EM_Hire_Date DATE, PRIMARY KEY(EM_Eno));
Create table Customers (Customers_Cno INT NOT NULL, Customers_Cname VARCHAR(50), Customers_Street VARCHAR(50), Customers_Zip INT, Customers_Phone INT, primary key(Customers_Cno));
Create table Orders (Orders_Ono INT NOT NULL, Orders_Cno INT, Orders_Eno INT, Orders_Received DATE, Orders_Shipped DATE, primary key(Orders_Ono));
insert into Orders values
(1,301,501,20161010,20161011);
(2,302,501,20161011,20161012);
(3,303,502,20161110,20161111);
(4,304,502,20161110,20161112);
(5,305,502,20161110,20161113);
(6,306,503,20161112,20161114);
(7,307,501,20161112,20161113);
(8,308,503,20161112,20161115);
(9,309,503,20161115,20161120);
(10,300,501,20161112,20161113);
insert into Customers values
(300,'Bryan','100 street',49009,1234567890),
(301,'Ryan','101 street',49008,1234567890),
(302,'Nick','102 street',49009,1234567890),
(303,'Nicholas','103 street',49009,1234567890),
(304,'Alexa','104 street',49009,1234567890),
(305,'Tori','105 street',49008,1234567890),
(306,'Scarlet','106 street',49008,1234567890),
(307,'Heather','100 street',49009,1234567890),
(308,'Amanda','107 street',49008,1234567890),
(309,'James','108 street',49008,1234567890);
insert into Employees values
(501,'Robert',20041010),
(502,'Sam',20050110),
(503,'Brandy',20050710);
2)理想的最終結果是回答查詢「列表的員工誰同時擁有49008拉鍊碼客戶和49009拉鍊碼客戶(名稱)。」
3)最佳嘗試迄今:
select Employees.EM_Ename
, Customers.Customers_Zip
from Employees
left
join Orders
on Employees.EM_Eno = Orders.Orders_Eno
left
join Customers
on Orders.Orders_Cno = Customers.Customers_Cno
group
by Employees.EM_Ename
, Customers.Customers_Zip;
感謝您對如何正確地請求指導。我相信我提供了一個更明確的要求。讓我知道如果它仍然不清楚。 –
其中是針對行集返回的值的過濾器,具有針對組對列的過濾器。 – Edward