简体   繁体   English

使用联接的SQL查询

[英]Sql Query Using Joins

I have 2 tables - student, exam 我有2张桌子-学生,考试

Student Table Schema: 学生表架构:

id  Name  Course
1   Kavi   Dr.
2   Priya  Engg.

Exam Table Schema: 考试表架构:

Examid Student_id  Exam Date
1        1          22-03-2014
2        2          23-04-2014
3        1          24-04-2014

My requirement is, i need to join these 2 tables,so that i can access all columns in that table. 我的要求是,我需要加入这2个表,以便可以访问该表中的所有列。 So far i have tried Query using Innerjoin but i get the result to be as 到目前为止,我已经尝试使用Innerjoin查询,但我得到的结果是

id  Name  Course
1   Kavi   Dr.
2   Priya  Engg.
1   1      22-03-2014
2   2      23-04-2014

But I need it like , 但我需要它,

id Name Course Exam Date
1  Kavi  Dr.    22-03-2014
1  Kavi  Dr.    24-04-2014
2  Priya Engg.  23-04-2014

Pls Help! 请帮助!

EDIT: 编辑:

SELECT *
FROM STUDENT S INNER JOIN
     EXAM E
     ON S.id=E.Student_id 

I'll give you the query, but first you should take a moment to understand how a select statement works. 我将给您查询,但首先您需要花一点时间来了解select语句的工作方式。

When you run a select statement you can imagine it as defining a completely temporary table that only exists for a moment while you're looking things up. 运行select语句时,您可以想象它是定义一个完全临时的表,该表仅在查找内容时存在。 This is better known as the results of your query. 这就是众所周知的查询结果。

The list of columns you select actually become the columns of your new table. 您选择的列列表实际上成为新表的列。 The "FROM" decides what tables are used to map those columns. “ FROM”决定使用哪些表来映射那些列。 The "WHERE" and "HAVING" clauses decide what rules the engine should use when determining the rows to include. “ WHERE”和“ HAVING”子句决定了在确定要包括的行时引擎应使用什么规则。

So, what columns do you want in your results? 那么,您想要结果中的哪些列?

  • student.id 学生卡
  • student.name 学生姓名
  • student.course 学生课程
  • exam.date 考试日期

You are selecting from the student and exam tables, but you want rows to be "merged" in terms of the student ID (this is where the JOIN comes in -- you should look at this for a better understanding of how JOIN works) 您正在从“学生”和“考试”表中进行选择,但是您希望根据学生ID将行“合并”(这是JOIN的用处-您应该查看此内容以更好地了解JOIN的工作方式)

Finally, it sounds like you want everything, so there is no WHERE / HAVING clause. 最后,听起来像您想要一切,所以没有WHERE / HAVING子句。

So your query should look like: 因此,您的查询应如下所示:

SELECT student.id, student.Name, student.Course, exam.Date
  FROM student
  JOIN exam ON student.id = exam.Student_id

Now, side note, you should keep a consistent naming structure for your columns. 现在,请注意,您应该为列保持一致的命名结构。 I suggest you always start lower case (so it would be student.course) and personally I avoid upper case letters in database definitions at all times. 我建议您始终以小写字母开头(因此应该是student.course),就我个人而言,我始终避免在数据库定义中使用大写字母。 At the very least, things starting with a capitol letter usually indicate a class or object name in the land of programming 至少,以大写字母开头的内容通常表示编程领域中的类或对象名称

It sounds like you're interested in dealing with duplicates. 听起来您有兴趣处理重复项。 You also want to look into key words like "DISTINCT" and "GROUP BY." 您还想研究关键词“ DISTINCT”和“ GROUP BY”。

Also... you need to provide a lot more information when asking a question. 另外...问问题时,您需要提供更多信息。 Explain what you have tried. 解释一下您尝试过的内容。 Explain all the requirements and the goals in an organized way. 有组织地解释所有要求和目标。 You get the idea. 你明白了。

Try this query 试试这个查询

SELECT s.*, e.exam_date FROM student_tbl s
JOIN exam_tbl e ON e.student_id = s.id

Try this one 试试这个

SELECT st.id,st.name,st.course,ex.date FROM student AS st 
LEFT JOIN exam AS ex ON ex.student_id=st.id

Try this Query: 试试这个查询:

SELECT Student.id ,Student.Name,Student.Course,`Exam.Exam Date`
FROM Student
INNER JOIN EXAM
ON Student.id=EXAM.Student_id;
SELECT
    S.id, S.Name, S.Course, `E.Exam Date`
FROM
    Student S, Exam E
WHERE
    S.id = E.Student_id

I still feel there's a serious design flaw on your tables - there's no table that defines the courses as entities from where to fetch the Course information (such as name). 我仍然觉得您的表上存在严重的设计缺陷-没有表将课程定义为从中获取课程信息(例如名称)的实体。 Some normalization would do you good, if you're able to change that. 如果您能够进行一些标准化,则对您有益。

I believe the problem with your query is subtle. 我相信您的查询问题很细微。 The query itself is correct, so this is what you want: 查询本身是正确的,因此这是您想要的:

SELECT *
FROM STUDENT S INNER JOIN
     EXAM E
     ON S.id=E.Student_id 

Somehow, though, you are getting a cross join rather than an inner join . 但是,不知何故,您得到的是cross join而不是inner join This could be happening because you are really executing: 这可能是因为您确实在执行:

SELECT *
FROM STUDENT S INNER JOIN
     EXAM E

In MySQL the on clause is optional, so you don't get an error. 在MySQL中, on子句是可选的,因此不会出错。 If so, just adding in the on clause should fix the problem. 如果是这样,只需添加on子句即可解决此问题。

Note: this could be cause by a semicolon in the query: 注意:这可能是由查询中的分号引起的:

SELECT *
FROM STUDENT S INNER JOIN
     EXAM E;
     ON S.id=E.Student_id 

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

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