我是 SQL 新手,我想编写一个查询来查找任何特定商店的唯一客户。 例如:SH1 商店有 10 名顾客注册,其中 10 - 3 名顾客还注册了其他一些商店。我想要一个查询,该查询将返回 SH1 商店的唯一 7 位客户。
ShopId
和 CustomerId
都存储在同一个表中,所以我猜需要一个子查询。
谢谢, 马尤尔
示例数据:
商店 ID 客户 ID
- 商店 1 Cust001
- 商店1 Cust002
- 商店 1 Cust003
- 商店2 Cust002
- Shop3 Cust004
- Shop4 Cust002
在上面的示例中,如果我对商店 ID Shop1 运行查询,我应该得到 Cust001、Cust003 作为返回,它们是 Shop1 所独有的,而不是 Cust002,因为它与其他商店 ID 链接。
最佳答案
我想你可以做到
select distinct customerId from tablename where shopId = 'SH1' and customerId not in (select customerId from tablename where shopId<>'SH1');
关于mysql - SQL:查找商店的唯一客户时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15380613/