帮助我们找到适合以下情况的操作员:
假设有一个表Employee
e_id | e_name | location
1 Mett New York
2 Bill Düsseldorf
3 Sam Portland
4 Dave Valencia
5 Max New York
6 Alex Portland
7 Andy Düsseldorf
8 John New York
问题:谁不在杜塞尔多夫或纽约工作?
在这种情况下你会使用运算符OR或AND吗?
我知道这是一个愚蠢的问题,但我一直在和这个问题的某个人争论......她说,我说,或者说。不知道现在要用什么。
谢谢!
答案 0 :(得分:5)
使用OR
是最容易理解的恕我直言,因为外NOT
容易传达所需的结果与(“伦敦”或“杜塞尔多夫”)相反
SELECT e_name FROM employee WHERE
NOT (location = 'New York' OR location = 'Düsseldorf')
这也很适合IN
运算符:
SELECT e_name FROM employee WHERE
location NOT IN ('New York', 'Düsseldorf')
然而 - 也可以用AND
表示:
SELECT e_name FROM employee WHERE
(location <> 'New York' AND location <> 'Düsseldorf')
差异取决于NOT
是应用于两个子句合并,还是分别应用于每个子句。这是De Morgan's Law的应用程序:
NOT (A OR B) == (NOT A) AND (NOT B)
答案 1 :(得分:5)
将NOT
添加到混音并调用DeMorgan's laws后,两个选项均可用:
最后,SQL提供了IN
运算符,它允许您将查询表示为“位置不在('Düsseldorf','New York')”
答案 2 :(得分:2)
使用@Scope(value = "step", proxyMode = ScopedProxyMode.INTERFACES)
@Component
public class MyItemReader implements ItemReader<Integer> {
@Autowired
private JobService jobService;
private List<Integer> itemsList;
private Long jobId;
@Autowired
public MyItemReader(@Value("#{jobParameters['jobId']}") final Long jobId) {
this.jobId = jobId;
this.itemsList = null;
}
@Override
public Integer read() throws Exception, UnexpectedInputException, ParseException, NonTransientResourceException {
// First pass: Load the list.
if (itemsList == null) {
itemsList = new ArrayList<Integer>();
MyJob myJob = (MyJob) jobService.loadById(jobId);
for (Integer i : myJob.getTargedIdList()) {
itemsList.add(i);
}
}
// Serve one at a time:
if (itemsList.isEmpty()) {
return null;
} else {
return itemsList.remove(0);
}
}
}
,您可以这样做:
AND
答案 3 :(得分:2)
AND
与NOTEQUALS
运算符一样。你可以像
select * from Employee WHERE location != "Düsseldorf" and location != "New York"
答案 4 :(得分:1)
选择,因为否定使它们可以互换!
NOT (location = 'Düsseldorf' OR location = 'New York'
-- is the same as
location <> 'Düsseldorf' AND location <> 'New York'