0

I have 2 tables - student, exam

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. So far i have tried Query using Innerjoin but i get the result to be as

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 
3
  • 1
    Please show that query that you have tried. Commented Sep 15, 2013 at 16:00
  • read this w3schools.com/sql/sql_join.asp Commented Sep 15, 2013 at 16:02
  • @GordonLinoff ..This is my query. SELECT * FROM STUDENT S INNER JOIN EXAM E ON S.id=E.Student_id Commented Sep 15, 2013 at 16:04

6 Answers 6

1

I'll give you the query, but first you should take a moment to understand how a select statement works.

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. 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. The "WHERE" and "HAVING" clauses decide what rules the engine should use when determining the rows to include.

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)

Finally, it sounds like you want everything, so there is no WHERE / HAVING clause.

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. 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."

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.

Sign up to request clarification or add additional context in comments.

Comments

0

Try this query

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

1 Comment

I have tried this,but the problem is that there may be many records in exam table for 1 student.
0

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

Comments

0
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.

Comments

0

Try this Query:

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

Comments

0

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. 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. If so, just adding in the on clause should fix the problem.

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 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.