简体   繁体   English

如何使用SQL连接从两个表中选择表

[英]How to select table from two tables using SQL join

I have to table in SQL course and student 我必须在SQL课程和学生中上桌

Course table has following fields 课程表具有以下字段

courseid coursename courseduration

Student table has the following fields 学生表具有以下字段

rollno  name  address courseid 

I want the result as following 我想要以下结果

 BCA
   abc
   def
   pqr
 MCA
   stu
   ijk
 BTECH
   xyz

I am applying inner join for it like following suggest me query for desired result My query is following: 我正在为其应用内部联接,如以下建议我查询所需结果我的查询如下:

SELECT student.name
FROM student
INNER JOIN course
ON student.courseid=course.courseid;

You can try this: 您可以尝试以下方法:

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;

This will give you something like this: 这会给你这样的东西:

Course_name   |  Student_name
BCA           |  abc,def,pqr
MCA           |  stu,ijk
BTECH         |  xyz

Here are some options 这里有一些选择

1) Include the course name in the select and order by it - it will be repeated on each row for the student in the course 1)将课程名称包括在选择和排序中-将在课程的每一行中重复该名称

SELECT course.name, student.name
FROM student
INNER JOIN course
ON student.courseid=course.courseid
ORDER BY course.name;

Which will return 哪个会回来

Course Student
 BCA   abc
 BCA   def
 BCA   pqr
 MCA   stu
 MCA   ijk
 BTECH  xyz

You would do the indentation in your presentation tier, eg by indenting every time the course name changes. 您可以在演示文稿层进行缩进,例如,每次课程名称更改时都进行缩进。

2) You can use GROUP_CONCAT to combine all students into a single column for a grouping on Course_name 2)您可以使用GROUP_CONCAT将所有学生合并到一个列中,以对Course_name进行分组

SELECT course.name, GROUP_CONCAT(student.name) AS Students
FROM student
INNER JOIN course
ON student.courseid=course.courseid
GROUP BY course.name;

Returns 返回

 Course  Students
 BCA     abc, def, pqr
 MCA     stu, ijk
 BTECH   xyz

Edit, re I just want one column 编辑,我只想要一栏

Well, you can project and combine the course name and student names with a UNION into a single column, and use an ordering hack to associate the students like this . 好吧,您可以将课程名称和学生姓名与UNION组合在一起,并投影到一个栏中,然后使用订购技巧将这样的学生联系起来 In fact, you can even add spaces to get the indentation effect. 实际上,您甚至可以添加空格以获得缩进效果。 But this is solving the wrong problem - this kind of formatting is presentation tier and shouldn't be done in Sql IMO. 但这解决了错误的问题-这种格式是表示层,不应在Sql IMO中完成。

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;

Returns 返回

BCA
 abc
 def
 pqr
BTECH
 xyz
MCA
 ijk
 stu

Try this 尝试这个

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM