如何在sql中正确使用内连接

时间:2012-08-06 14:38:31

标签: sql database oracle

我有两张桌子:

离开

debt city
43   odesa
23   kiev
79   kiev
78   donezk

empl

ide   fn    ln      debt
3421  jed   trt       43
 354  jed   res       43
  43  ged   hjkhg     79
  73  ghghg gfgf      79
 456  jkl   gdfg      78
 532  kkhg  vjv       23
  45  ki    vt        79
 243  ki    vt        78

我需要选择每个部门(fn)的行数(debt)并按城市排序。我写了这段代码:

select empl.DEBT, count (*) as emp_num 
from depart 
inner join empl on empl.DEBT = depart.DEBT
group by empl.DEBT
order by depart.CITY;

但是这段代码不起作用。我做错了什么?

我使用Oracle db,在Toad中执行脚本。

错误讯息是

  

发生以下错误:ORA-00900:无效的SQL语句

2 个答案:

答案 0 :(得分:2)

要按城市订购,您还需要按城市分组:

select empl.DEBT, count (*) as emp_num 
from depart 
inner join empl 
on empl.DEBT = depart.DEBT
group by empl.DEBT, depart.CITY
order by depart.CITY;

答案 1 :(得分:1)

select empl.DEBT, count (*) as emp_num 
from depart 
inner join empl on empl.DEBT = depart.DEBT
group by depart.CITY,empl.DEBT
order by depart.CITY;