0

我有两个表,例如EmployeeProject表:

Employee (id, dept, joining_date)
Project (emp_id, project)

从表Project中获得外键。Employee我必须查询project并按他们的顺序dept返回。哪个查询将在以下查询的大数据集上运行得更快?Employeejoining_date

select * from Employee where id in (select p.emp_id from Project p join Employee e on p.emp_id = e.id where p.project = 'project1' and e.dept = 'dept1') order by joining_date

select * from Employee where id in (select p.emp_id from Project p join Employee e on p.emp_id = e.id where p.project = 'project1' and e.dept = 'dept1') and dept = 'dept1' order by joining_date

或者有没有更好更简单的方法来做到这一点?

4

1 回答 1

0

使用IN()表达式的外部查询毫无用处是完全没有必要的。这将产生您需要的输出:

select e.*
from Project p 
inner join Employee e on p.emp_id = e.id 
where p.project = 'project1' and e.dept = 'dept1'
order by joining_date
于 2016-03-04T03:09:52.077 回答