我必须参加SQL课程和学生
课程表包含以下字段
courseid coursename courseduration
学生表包含以下字段
rollno name address courseid
我希望结果如下
BCA
abc
def
pqr
MCA
stu
ijk
BTECH
xyz
我正在应用内部联接,如下面建议我查询所需的结果 我的查询如下:
SELECT student.name
FROM student
INNER JOIN course
ON student.courseid=course.courseid;
答案 0 :(得分:1)
你可以试试这个:
BCA
abc
def
pqr
BTECH
xyz
MCA
ijk
stu
这会给你这样的东西:
SELECT
course.name AS course_name,
GROUP_CONCAT(student.name) AS student_name
FROM student
INNER JOIN course
ON student.courseid = course.courseid
GROUP BY course.courseid;
答案 1 :(得分:0)
以下是一些选项
1)在选择和顺序中包括课程名称 - 课程中的学生将在每一行重复该课程名称
scanf("%c%*c", &ch);
将返回
SELECT course.name, student.name
FROM student
INNER JOIN course
ON student.courseid=course.courseid
ORDER BY course.name;
您可以在演示文稿层中进行缩进,例如每次课程名称更改时缩进。
2)您可以使用Course Student
BCA abc
BCA def
BCA pqr
MCA stu
MCA ijk
BTECH xyz
将所有学生合并为一个列,以便在Course_name上进行分组
GROUP_CONCAT
返回
SELECT course.name, GROUP_CONCAT(student.name) AS Students
FROM student
INNER JOIN course
ON student.courseid=course.courseid
GROUP BY course.name;
编辑,我只想要一列
好吧,您可以将课程名称和学生姓名与 Course Students
BCA abc, def, pqr
MCA stu, ijk
BTECH xyz
投影并合并到一个列中,并使用排序黑客来关联学生like this。实际上,您甚至可以添加空格来获得缩进效果。但这解决了错误的问题 - 这种格式是表示层,不应该在Sql IMO中完成。
UNION
返回
SELECT Name
FROM
(
SELECT course.coursename AS Name, course.coursename AS Course, 1 As RowType
FROM course
UNION ALL
SELECT CONCAT(' ', student.name), course.coursename, 2 AS RowType
FROM student
INNER JOIN course
ON student.courseid=course.courseid
) x
ORDER BY Course, RowType;
答案 2 :(得分:0)
试试这个
SELECT `name`
FROM(
SELECT course.courseid, course.coursename AS `name`
FROM course
UNION
SELECT student.courseid + 0.1 AS courseid, courseid.name AS `name`
FROM student
ORDER BY courseid ASC) AS ordered_list