假设您有两个表...表1和表2。
表1列是名称/年龄/区域
表2有区/职称
我想选择区域和职称,只要该区域包括'sarah'和'Phillip'两个名称(必须包括给定名称)
答案 0 :(得分:2)
另一种方法不如聚合方法灵活。但是,您有两个表,它可能具有更好的性能:
public class Meter
{
public String regNum;
public String workOrNot;
public String location;
/**
* Constructor for objects of class Clock
*/
public Meter(String regNum, String workOrNot, String location)
{
// initialise instance variables
setRegNum(regNum);
setWorkOrNot(workOrNot);
setLocation(location);
}
//REGISTRATION NUMBER
public void setRegNum(String regNum){
this.regNum = regNum;
}
public String getRegNum(){
return regNum;
}
//WORK OR NOT
public void setWorkOrNot(String workOrNot){
this.workOrNot = workOrNot;
}
public String getWorkOrNot(){
return workOrNot;
}
//LOCATION
public void setLocation(String location){
this.location = location;
}
public String getLocation(){
return location;
}
}
特别是,这可以利用select t2.*
from table2 t2
where exists (select 1 from table1 t1 where t1.area = t2.area and t1.name = 'sarah') and
exists (select 1 from table1 t1 where t1.area = t2.area and t1.name = 'Phillip');
上的索引。
答案 1 :(得分:0)
如果你对table1(区域,名称)有一个唯一的约束,并且如果你在使用更一般的答案时遇到性能问题,你可以尝试连接 - 数据库引擎有时可以比相关的子查询优化更多: / p>
select t2.*
from table2 t2
join table1 t1a on t1a.area = t2.area and t1a.name = 'sarah'
join table1 t1b on t1b.area = t2.area and t1b.name = 'Phillip';