gpt4 book ai didi

mysql - 使用 IN 时比较两个 SQL 查询

转载 作者:行者123 更新时间:2023-11-29 21:19:11 25 4
gpt4 key购买 nike

我有两个表,例如 EmployeeProject 表:

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

Project 具有来自 Employee 表的外键。我必须查询 projectdept 并按照 joining_date 的顺序返回 Employee。哪个查询在以下查询的大数据集上运行得更快?

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

或者有没有更好更简单的方法?

最佳答案

使用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

关于mysql - 使用 IN 时比较两个 SQL 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35787200/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com