查询微调

时间:2017-06-05 10:06:13

标签: mysql

我是sql的新手,想请求您对此查询的帮助。还有另一种方法可以重写吗?

 Select *
 From emp
 Where emp_no IN (
     Select emp_no
     From dept_emp
     Where dept_no = 'd002'
     );

非常感谢任何帮助。

感谢。

4 个答案:

答案 0 :(得分:1)

您可以使用/myscripts

exists

也许select * from emp where exists( select 1 from dept_emp where dept_emp.emp_no = emp.emp_no and dept_no = 'd002' ) 可以工作:

inner join

答案 1 :(得分:0)

您可以使用内部联接

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

) t on emp_no.id = t.emp_no

或没有subselect

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

答案 2 :(得分:0)

你可以像这样使用连接:

select * from emp e, dept_emp d where e.emp_no = d.emp_no and d.dept_no = 'd002'

答案 3 :(得分:0)

 - 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.