一个位置的项目不在另一个位置。 SQL

问题描述 投票:0回答:2

我正在尝试查询,可能是通过自我加入,查找哪些产品在芝加哥销售,而这些产品在迈阿密没有销售。

ProductID           Product_Type  On Sale Stock_location
2201                   Cereal         Y   chicago
2202                   Beverage       Y   chicago
2203                   Frozen Food    y   chicago
2204                   Poultry        N   chicago
2205                   Health         N   chicago
2206                    Snacks        Y   chicago
2207                   Household      N   chicago
2208                   Personal       N   chicago
2209                   Produce        N   chicago
2201                    Cereal        Y   Miami
2202                    Beverage      Y   Miami
2203                    Frozen Food   N   Miami
2204                    Poultry       Y   Miami
2205                    Health        N   Miami
2206                   Snacks         Y   Miami
2207                    Household     N   Miami
2208                    Personal      N   Miami 
sql join
2个回答
0
投票
SELECT t1.*
  FROM t AS t1
  JOIN t AS t2 ON ( t1.Product_Type = t2.Product_Type )
 WHERE t1.On_Sale = "Y"
   AND t2.On_Sale = "N"; 

有用的资源:https://www.w3schools.com/sql/sql_join_self.asp


1
投票

它就像下面这样

select Product_Type from Products 
where [On Sale] = 'Y' and 
Stock_location = 'chicago' and 
Product_Type not in (Select Product_Type form Products where [On Sale] = 'N' and Stock_location = 'Miami' )
© www.soinside.com 2019 - 2024. All rights reserved.