Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 查询微调_Mysql - Fatal编程技术网

Mysql 查询微调

Mysql 查询微调,mysql,Mysql,我是sql的新手,希望您在这个问题上给予帮助。有没有其他方法重写这个 Select * From emp Where emp_no IN ( Select emp_no From dept_emp Where dept_no = 'd002' ); 非常感谢您的帮助 谢谢。您可以使用exists: select * from emp where exists( select 1 from dept_emp where dept_emp.e

我是sql的新手,希望您在这个问题上给予帮助。有没有其他方法重写这个

 Select *
 From emp
 Where emp_no IN (
     Select emp_no
     From dept_emp
     Where dept_no = 'd002'
     );
非常感谢您的帮助


谢谢。

您可以使用
exists

select * 
from emp
where exists(
    select 1 from dept_emp where dept_emp.emp_no = emp.emp_no and dept_no = 'd002'
)
也可能
内部连接
工作:

select emp.*
from emp
join dept_emp
on dept_emp.emp_no = emp.emp_no
and dept_no = 'd002'

可以使用内部联接

select * 
from emp
inner join (
    Select emp_no
     From dept_emp
     Where dept_no = 'd002'

) t on emp_no.id = t.emp_no
或无子选择

select * 
from emp
inner join dept_emp on emp_no.id = dept_emp.emp_no 
   and dept_emp.dept_no = 'd002'

您可以像这样使用join:

从emp e、dept_emp d中选择*其中e.emp_no=d.emp_no和d.dept_no='d002'

- If have the relation between two tables best-way you can used join. other wise you can used inner query (sub select) - When you used join in RDBMS it will faster that inner query. - join must be indexed based like: primary key and foreign key. Other wise executing cost will be high. non-index query time consumed maximum. -如果有两个表之间的关系,最好的方法就是使用join。其他方面,您可以使用内部查询(子选择) -当您在RDBMS中使用join时,内部查询会更快。 -联接必须基于索引,例如:主键和外键。其他明智的执行成本将很高。最大消耗的非索引查询时间。
为什么
不存在
?没有问题。因为你的原因,我不得不再次核对我的答案P请您解释一下这两个问题是如何工作的,这样我就知道为什么它比我以前的问题好了。您知道,您的查询很有效,而且它更明显。我的第一个查询是查询存在于
dept\u emp
dept\u no='d002'
中的emp,我的第二个查询使用
内部连接
获取两个表中存在的emp,然后使用
dept\u no='d002'
过滤这些emp。