如何使用IN关键字?

加内什

我有一个表PropertyFeatures该表具有三列。第一个PropertyFeatureId是自动递增的列,第二个PropertyidProperty的外键,最后一个FeatureidFeature的外键

+------------------------------------------+
| PropertyFeatureId  PropertyId  FeatureId |
+------------------------------------------+
| 1                   1             1      |
| 2                   1             2      |
| 3                   2             2      |
| 4                   2             3      |
| 5                   2             4      |
+------------------------------------------+

SELECT propertyFeatures.PropertyId       
FROM PropertyFeatures propertyFeatures INNER JOIN Feature feature ON feature.id = propertyFeatures.FeatureId
WHERE propertyFeatures.[FeatureId] IN (1,2)
GROUP BY propertyFeatures.PropertyId

上面的查询给出以下结果:

PropertyId 
1
2

我想得到以下结果:

PropertyId
1

因为Featureid1和2仅适用于PropertyId1。

如果我将查询更改为以下内容:

SELECT propertyFeatures.PropertyId       
FROM PropertyFeatures propertyFeatures INNER JOIN Feature feature ON feature.id = propertyFeatures.FeatureId
WHERE propertyFeatures .[FeatureId] IN (2,3,4)
GROUP BY propertyFeatures.PropertyId

它只会显示:

PropertyId 
2 

因为Featureid2、3和4仅适用于PropertyId2。

如何获得理想的结果?

乔森

请记住,这WHERE a IN (1, 2)实际上是的快捷方式WHERE a = 1 OR a = 2这不会给您两个集合的交集,只有第一个过滤器有效的集合与第二个过滤器有效的集合的交集。在您的情况下,EXISTS您要搜索的是-或INTERSECT

范例INTERSECT

SELECT   propertyFeatures.PropertyId       
FROM     PropertyFeatures AS propertyFeatures
INNER JOIN Feature AS feature ON feature.id = propertyFeatures.FeatureId
WHERE    propertyFeatures.[FeatureId] = 1
GROUP BY propertyFeatures.PropertyId
INTERSECT
SELECT   propertyFeatures.PropertyId       
FROM     PropertyFeatures AS propertyFeatures
INNER JOIN Feature AS feature ON feature.id = propertyFeatures.FeatureId
WHERE    propertyFeatures.[FeatureId] = 2
GROUP BY propertyFeatures.PropertyId;

范例EXISTS

SELECT   propertyFeatures.PropertyId       
FROM     PropertyFeatures AS propertyFeatures
INNER JOIN Feature AS feature ON feature.id = propertyFeatures.FeatureId
WHERE    EXISTS (SELECT TOP 1 * FROM PropertyFeatures AS P
                 INNER JOIN Feature AS F ON F.id = P.FeatureId
                 WHERE P.FeatureId = 1)
         AND
         EXISTS (SELECT TOP 1 * FROM PropertyFeatures AS P
                 INNER JOIN Feature AS F ON F.id = P.FeatureId
                 WHERE P.FeatureId = 2);

(代码未经测试,因此请自行检查是否有效!)

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何使用await关键字?

来自分类Dev

如何使用SQL MINUS关键字

来自分类Dev

如何使用“单独”关键字

来自分类Dev

如何使用'with'关键字添加?

来自分类Dev

如何使用“ this”关键字作为参数?

来自分类Dev

d3js-如何使用`this`关键字或替代关键字是什么?

来自分类Dev

Scala:如何使用不带match关键字的case关键字?

来自分类Dev

如何使用某些关键字查找包含这些关键字的文章?

来自分类Dev

d3js-如何使用`this`关键字或替代关键字是什么?

来自分类Dev

elasticsearch如何使用精确搜索并忽略关键字中的关键字特殊字符?

来自分类Dev

“ is”关键字如何工作?

来自分类Dev

如何开发关键字?

来自分类Dev

“ this”关键字如何工作?

来自分类Dev

使用IN关键字查询

来自分类Dev

使用或过滤关键字

来自分类Dev

JavaScript 使用 (this) 关键字

来自分类Dev

@final 关键字的使用

来自分类Dev

App Store关键字:使用“商标”关键字

来自分类Dev

如何在本地反应上使用等待关键字?

来自分类Dev

如何在Scala中使用“元组”关键字?

来自分类Dev

如何使用Watson NLP与JS分析关键字?

来自分类Dev

如何使用VsVim在多个文件中查找关键字

来自分类Dev

在Ansible中,如何使用环境关键字?

来自分类Dev

如何在模板类中使用friend关键字

来自分类Dev

如何在Haskell中使用“ aux”关键字

来自分类Dev

如何使用PHP检索YouTube视频关键字

来自分类Dev

如何使用Spark在文本表中查找关键字?

来自分类Dev

如何使用Javascript关键字作为变量名?

来自分类Dev

如何使用“ |” Specflow功能表中的关键字