从两个表中获取数据?

发布于 2024-12-03 10:51:59 字数 1708 浏览 4 评论 0原文

我试图从两个不同的表获取数据并放入 on 语句中,但它不起作用。这就是我希望得到的完整语句:我希望查询显示 dname、loc、人数。我的子查询有问题。

 SQL> select dname, loc from dept where ename in (count(ename) AS Number_of_People  from emp);
select dname, loc from dept where ename in (count(ename) AS Number_of_People from emp)
                                        *
ERROR at line 1:
 ORA-00934: group function is not allowed here


SQL>

表 emp

SQL> select empno, ename, job, hiredate, deptno from emp;

 EMPNO ENAME      JOB       HIREDATE      DEPTNO
 ---------- ---------- --------- --------- ----------
  7839 KING       PRESIDENT 17-NOV-81         10
  7698 BLAKE      MANAGER   01-MAY-81         30
  7782 CLARK      MANAGER   09-JUN-81         10
  7566 JONES      MANAGER   02-APR-81         20
  7654 MARTIN     SALESMAN  28-SEP-81         30
  7499 ALLEN      SALESMAN  20-FEB-81         30
  7844 TURNER     SALESMAN  08-SEP-81         30
  7900 JAMES      CLERK     03-DEC-81         30
  7521 WARD       SALESMAN  22-FEB-81         30
  7902 FORD       ANALYST   03-DEC-81         20
  7369 SMITH      CLERK     17-DEC-80         20

 EMPNO ENAME      JOB       HIREDATE      DEPTNO
 ---------- ---------- --------- --------- ----------
  7788 SCOTT      ANALYST   09-DEC-82         20
  7876 ADAMS      CLERK     12-JAN-83         20
  7934 MILLER     CLERK     23-JAN-82         10

 14 rows selected.

 SQL>

表 dept

 SQL> select * from dept;

 DEPTNO DNAME          LOC
  ---------- -------------- -------------
    10 ACCOUNTING     NEW YORK
    20 RESEARCH       DALLAS
    30 SALES          CHICAGO
    40 OPERATIONS     BOSTON

   SQL>  

I am trying to get data from two different table and put into on statement but its not working. This is what I am looking to get as a complete statement: I want the query to display the dname, loc, Number of People. I am having a problems with the sub query.

 SQL> select dname, loc from dept where ename in (count(ename) AS Number_of_People  from emp);
select dname, loc from dept where ename in (count(ename) AS Number_of_People from emp)
                                        *
ERROR at line 1:
 ORA-00934: group function is not allowed here


SQL>

Table emp

SQL> select empno, ename, job, hiredate, deptno from emp;

 EMPNO ENAME      JOB       HIREDATE      DEPTNO
 ---------- ---------- --------- --------- ----------
  7839 KING       PRESIDENT 17-NOV-81         10
  7698 BLAKE      MANAGER   01-MAY-81         30
  7782 CLARK      MANAGER   09-JUN-81         10
  7566 JONES      MANAGER   02-APR-81         20
  7654 MARTIN     SALESMAN  28-SEP-81         30
  7499 ALLEN      SALESMAN  20-FEB-81         30
  7844 TURNER     SALESMAN  08-SEP-81         30
  7900 JAMES      CLERK     03-DEC-81         30
  7521 WARD       SALESMAN  22-FEB-81         30
  7902 FORD       ANALYST   03-DEC-81         20
  7369 SMITH      CLERK     17-DEC-80         20

 EMPNO ENAME      JOB       HIREDATE      DEPTNO
 ---------- ---------- --------- --------- ----------
  7788 SCOTT      ANALYST   09-DEC-82         20
  7876 ADAMS      CLERK     12-JAN-83         20
  7934 MILLER     CLERK     23-JAN-82         10

 14 rows selected.

 SQL>

Table dept

 SQL> select * from dept;

 DEPTNO DNAME          LOC
  ---------- -------------- -------------
    10 ACCOUNTING     NEW YORK
    20 RESEARCH       DALLAS
    30 SALES          CHICAGO
    40 OPERATIONS     BOSTON

   SQL>  

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

并安 2024-12-10 10:51:59

你想实现这样的目标吗?

select dname, loc, (select count(ename) from emp where DEPTNO = dept.deptno) as Number_of_people 
from dept;

Are you trying to achieve something like this?

select dname, loc, (select count(ename) from emp where DEPTNO = dept.deptno) as Number_of_people 
from dept;
月亮邮递员 2024-12-10 10:51:59

我不确定您要做什么,但突出的是您可能需要在子查询中使用 select 。尝试:

select dname, loc 
from dept 
where ename in (select count(ename) AS Number_of_People  from emp);

I'm not sure what you're trying to do, but something that sticks out is that you probably need to use a select in your subquery. Try:

select dname, loc 
from dept 
where ename in (select count(ename) AS Number_of_People  from emp);
笑梦风尘 2024-12-10 10:51:59

试试这个

select dept.dname, dept.loc,count(*) 
from emp 
join dept on emp.deptNo=dept.deptno
group by dept.dname,dept.loc

Try this

select dept.dname, dept.loc,count(*) 
from emp 
join dept on emp.deptNo=dept.deptno
group by dept.dname,dept.loc
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文