我有2个表emp和salgrade,其架构定义为: emp(empno,ename,sal,mgr)和salgrade(grade,losal,hisal)
查询
列出在国王下工作的最高级的英雄,等级超过3。
我已经按照以下说明编写了这个,首先提取所有在KING下工作的员工,等级> 3,然后从中提取最资深的员工及其雇员,但它给我语法错误,而不是单一组-group功能,请指导我哪里做错了?
<ul class="clearfix">
<div id="ubercontainer">
<div id="container">
<img src="../images/sideGAME1.jpg" / class="sideGMimg5">
<li class="bounce animated ">
<img src="../images/sideGAME2.jpg " / class="sideGMimg1 ">
<span class="Jacpots_1">Major Millions</span>
<br />
<span id="firstword" class="introchange1">$6 231 515.23</span>
</li>
<li class="bounce2 animated2">
<img src="../images/sideGAME3.jpg" / class="sideGMimg2">
<span class="Jacpots_2 ">Mega Moolah</span>
<br />
<span id="secondword " class="introchange2 ">$6 231 515.23</span>
</li>
<li class="bounce3 animated3 ">
<img src="../images/sideGAME4.jpg " / class="sideGMimg3 ">
<span class="Jacpots_3">Mega Moolah Isis</span>
<br />
<span id="thirdword" class="introchange3">$6 231 515.23</span>
</li>
</div>
</div>
<!-- <span class="Jacpots_1">abc</span> -->
</ul>
答案 0 :(得分:2)
添加Group by
以查找每个Hiredate
ename
SELECT ename,
Min(hiredate)
FROM (SELECT ename,
hiredate
FROM emp,
salgrade
WHERE mgr = (SELECT empno
FROM emp
WHERE ename = 'KING')
AND salgrade.grade > 3
AND emp.sal BETWEEN salgrade.losal AND salgrade.hisal)
GROUP BY ename --Here
查询可以像这样简化
SELECT ename,
Min(hiredate)
FROM emp
JOIN salgrade
ON mgr = empno
AND emp.sal BETWEEN salgrade.losal AND salgrade.hisal
WHERE ename = 'KING'
AND salgrade.grade > 3
GROUP BY ename
更新:要查找最资深的员工,请使用Order by
和ROWNUM
SELECT *
FROM (SELECT *
FROM emp
join salgrade
ON mgr = empno
AND emp.sal BETWEEN salgrade.losal AND salgrade.hisal
WHERE ename = 'KING'
AND salgrade.grade > 3
ORDER BY hiredate ASC)
WHERE ROWNUM = 1