sql - 如何查找不同表中的行数

标签 sql postgresql

我想从两个不同的表中获取数据,一个表包含学生总数,另一个表包含特定学生信息我如何获得学生人数 我想显示 name , code, totalstudent 和 no.of ngo student

select 
       a.name as name, a.school_code as CODE, 
       a.num_of_student as totalstudent,
       b.COUNT (ngo_student_name) as total_student 
from 
      ngo_student as a 
      INNER JOIN student_details as b on a.name=b.ngo_student_name 
GROUP BY
      b.ngo_student_name

此查询显示错误,请指导我 谢谢

最佳答案

尝试以下 - 您的 count(b.ngo_student_name) 而不是 b.count(ngo_student_name) 以及选择列表中的其他列应该在 group by 子句中

select 
       a.name as name, a.school_code as CODE, 
       count(a.num_of_student) as totalstudent,
       COUNT(b.ngo_student_name) as total_student 
from 
      ngo_student as a 
      INNER JOIN student_details as b on a.name=b.ngo_student_name 
GROUP BY
      a.name,a.school_code

关于sql - 如何查找不同表中的行数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53004215/

相关文章:

.net - linq 等效 SQL 查询 "not in (select query)"

mysql - 在两列中查找具有最大匹配字符串的行

python - 使用 API 访问 sqlalchemy 中的关系表数据

PostgreSQL - 将列数据类型从整数更改为整数数组

postgresql - 是否可以为 postgresql 从站添加额外的可写表(由 Skytools/londiste 复制)

sql - Postgres删除所有重复记录,但通过排序删除一条记录

java - SQL select 语句无法正确打印

sql - 清空自引用 MySQL 表的最佳方法是什么?

sql - 使用 CURRENT_TIMESTAMP 查询时间戳分区表的效率

postgresql - SQL 触发器如何影响性能?