Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
不使用子查询返回计数的简单SQL_Sql_Postgresql - Fatal编程技术网

不使用子查询返回计数的简单SQL

不使用子查询返回计数的简单SQL,sql,postgresql,Sql,Postgresql,我试图通过一条SQL语句获取注册课程的学生人数,但不使用子查询。到目前为止,我只知道如何使用子查询。还有别的办法吗 考虑以下数据库设置: create table student (id integer not null primary key); create table course_enrolment (student integer not null references student, course integer not null); insert into student val

我试图通过一条SQL语句获取注册课程的学生人数,但不使用子查询。到目前为止,我只知道如何使用子查询。还有别的办法吗

考虑以下数据库设置:

create table student (id integer not null primary key); create table course_enrolment (student integer not null references student, course integer not null); insert into student values (1); insert into student values (2); insert into student values (3); insert into course_enrolment values (2,20); insert into course_enrolment values (2,30); insert into course_enrolment values (3,10); 我想知道参加课程的学生人数。在这种情况下,它是2

我可以使用子查询轻松实现这一点:

从从课程注册中选择不同的学生作为数据选择计数*

我想在不使用子查询的情况下获取计数

我正在使用Postgresql 8.3,但我正在寻找一种与供应商无关的解决方案。

这个怎么样:

SELECT  course, COUNT(DISTINCT student)
FROM    course_enrolment
GROUP BY course
这为学生提供了每门课程。如果您只需要任何课程的注册总数:

SELECT  COUNT(DISTINCT student)
FROM    course_enrolment
我相信这都是ANSI标准SQL,所以在大多数情况下都应该适用。

这个怎么样:

SELECT  course, COUNT(DISTINCT student)
FROM    course_enrolment
GROUP BY course
SELECT COUNT(DISTINCT STUDENT) FROM COURSE_ENROLMENT
这为学生提供了每门课程。如果您只需要任何课程的注册总数:

SELECT  COUNT(DISTINCT student)
FROM    course_enrolment

我相信这都是ANSI标准SQL,所以应该适用于大多数地方。

我不知道postgres,但在SQL Server上:

SELECT COUNT(DISTINCT STUDENT) FROM COURSE_ENROLMENT
SELECT COUNT(DISTINCT STUDENT) FROM COURSE_ENROLMENT

我不知道postgres,但在SQL Server上:

SELECT COUNT(DISTINCT STUDENT) FROM COURSE_ENROLMENT

这是按课程选择学生:

select course, count(*) as students 
from course_enrolment
group by course
这只是为了计算学生人数,希望不是可怕的SQL Server特定的

select count(distinct student) from course_enrolment

这是按课程选择学生:

select course, count(*) as students 
from course_enrolment
group by course
这只是为了计算学生人数,希望不是可怕的SQL Server特定的

select count(distinct student) from course_enrolment

我对Postgresql了解不多,但这就是我在MS SQL Server上的做法

select count(distinct student)
From course_enrolment

我对Postgresql了解不多,但这就是我在MS SQL Server上的做法

select count(distinct student)
From course_enrolment

真正的问题是为什么不想使用子查询

一般来说,每个人的回答都是正确的:

select count(distinct student) from course_enrolment
但是在8.4版本之前的PostgreSQL中,这种类型的查询对于大量学生/课程来说会比较慢。更快的方法是:

select count(*) from (select student from course_enrolment group by student);

但它使用子查询——出于某种原因,您不想使用子查询。

真正的问题是,您为什么不想使用子查询

select COUNT(distinct student) from course_enrolment
一般来说,每个人的回答都是正确的:

select count(distinct student) from course_enrolment
但是在8.4版本之前的PostgreSQL中,这种类型的查询对于大量学生/课程来说会比较慢。更快的方法是:

select count(*) from (select student from course_enrolment group by student);

但它使用子查询——出于某种原因,您不希望使用子查询。

您也可以使用u;你也可以用u来拼写颜色;
select COUNT(distinct student) from course_enrolment