Sql server 我需要连接两个表以从student_info表中获取所有记录
我需要连接两个表,以获取来自student_info表的所有记录,以及来自student_activities表的记录,其中student_id相等 由于单个学生id的学生活动表中可能有多条记录,因此当我使用左联接打印输出时,会得到重复的记录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 有人建议我使用以下内容
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