Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/25.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 server 我需要连接两个表以从student_info表中获取所有记录_Sql Server_Sql - Fatal编程技术网

Sql server 我需要连接两个表以从student_info表中获取所有记录

Sql server 我需要连接两个表以从student_info表中获取所有记录,sql-server,sql,Sql Server,Sql,我需要连接两个表,以获取来自student_info表的所有记录,以及来自student_activities表的记录,其中student_id相等 由于单个学生id的学生活动表中可能有多条记录,因此当我使用左联接打印输出时,会得到重复的记录 SELECT * FROM student_info LEFT JOIN student_activities ON student_info.student_id=student_activities.student _id 有人建议我使用以下内容

我需要连接两个表,以获取来自student_info表的所有记录,以及来自student_activities表的记录,其中student_id相等

由于单个学生id的学生活动表中可能有多条记录,因此当我使用左联接打印输出时,会得到重复的记录

SELECT *
FROM student_info 
LEFT JOIN student_activities 
ON student_info.student_id=student_activities.student _id
有人建议我使用以下内容,但我得到的错误是,特定字段不是聚合函数的一部分

SELECT student_info.student_id, student_info.student_name, student_info.phone, student_info.age, COUNT (student_activities.student_id) AS COA
FROM student_info 
LEFT OUTER JOIN student_activities 
ON student_info.student_id=student_activities.student_id 
GROUP BY student_info.student_id

未经测试,但应能正常工作或类似以下情况:

SELECT DISTINCT *
FROM student_info 
WHERE student_info.student_id = 
(SELECT student_activities.student _id FROM student_activities)
SELECT DISTINCT *
FROM student_info 
WHERE student_info.student_id = 
(SELECT student_activities.student _id FROM student_activities)
SELECT 
 student_info.student_id, 
 student_info.student_name, 
 student_info.phone, 
 student_info.age, 
 COUNT (student_activities.student_id) AS COA
FROM student_info LEFT OUTER JOIN student_activities 
ON student_info.student_id=student_activities.student_id 
GROUP BY student_info.student_id,student_info.student_name, student_info.phone, student_info.age