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

标签 mysql sql sql-order-by where-clause sql-in

我有两个表,例如 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/

相关文章:

php - 使用 mySQL 在 PHP 中 SQL 不返回结果

php - SQLSTATE[23000] : Integrity constraint violation: 1052 using join with laravel

Mysql "OR"查询按条件顺序获取行

sql - 我需要知道如何创建交叉表查询

php - MySQL 查询输出的决策

MySQL 枢轴查询

php - 错误 310 (net::ERR_TOO_MANY_REDIRECTS):重定向太多。使用 PHP

c# - 无法使用 OrderBy C# 功能以某种方式对列表进行排序

mysql - Magento 2.1.7订单保存错误: SQLSTATE[42S02]:

mysql - 分页sql查询语法