where子句中的多个PK

问题描述

在我的表中,我有LociD)这是我的PK,我试图将其范围缩小到特定的4,但是当我在下面运行查询时,我仅得到一个答案。该查询需要解决什么?

-- second we find out the name of the locations 

select name from location where Locid = 524 and 512 and 505 and 506 ; 

解决方法

使用WHERE IN可以更好地表达您要编写的查询:

select name from location where locid = in (524,512,505,506);

当前查询中实际发生的情况是,初始值(locid)后面的 上的524值被解释为字面真实。因此,您的查询与此相同:

select name from location where locid = 524 and true and true and true;

这当然与:

select name from location where locid = 524;

也就是说,您只会从匹配的524中获取记录。