简体   繁体   中英

mysql: sub-query return more than 1 rows

I was trying to solve this question given below by using the subquery in MySQL but can't find a solution as it's showing subquery returns more than one row.

Problem Statement: Display the emp name with their salaries and their managers and managers salary.
Display those only employees whose managers were hired after them.

select w.ename employee, w.SAL woSal, m.ename manager, m.SAL mSal
from emp w, emp m
where w.mgr = m.empno and
(SELECT E1.ENAME  
FROM EMP E1, EMP E2
WHERE E1.MGR=E2.EMPNO and ( E1.hiredate<E2.hiredate));

You don't need a subquery - just add the condition on the hiredate to the join condition. Also note that implicit joins (using more than one table in the from clause) is considered deprecated, and you'd probably be better off rewriting it as an explicit join:

SELECT w.ename employee, w.SAL woSal, m.ename manager, m.SAL mSal
FROM   emp w
JOIN   emp m
WHERE  w.mgr = m.empno and w.hiredate < m.hiredate

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM