我有两个表,例如Employee
和Project
表:
Employee (id, dept, joining_date)
Project (emp_id, project)
Project
具有来自Employee
表的外键。
我必须查询project
和dept
,然后按Employee
的顺序返回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') 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
或者有更好更简单的方法吗?
答案 0 :(得分: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