0

Following is my tables which is student id is common field in both tables. I want to get both tables data in single query.Also get recent data of student.

 table A:

        student_id    name     surname        email
        ------------------------------------------------
              1       ABC       LLL       [email protected]
              2       PQR       SSS       [email protected]


        Table B:

          student_id  Assignment_Id  Assignment_Name      last_submited   
         ---------------------------------------------------------------------
              2            1             asign_1            sub_0001
              1            2             asign_2            sub_0002 
              2            3             asign_2            sub_0003

I want exact output like:-

student_id   Assignment_Id         email      last_submited
--------------------------------------------------------------
    2             3               [email protected]      sub_0003

I have used following query for getting recent record but confused how to get email id along with this.

SELECT assignment_id,
       student_id,
       last_submited
FROM tableB
WHERE student_id= '2'
ORDER BY assignment_id DESC LIMIT 1
1
  • mysql or sql server ... can't be both Commented Apr 16, 2014 at 8:06

3 Answers 3

2

You can make use of a JOIN

select  tableB.assignment_id, 
        tableB.student_id, 
        tableB.last_submited,
        tableA.email
from    tableB INNER JOIN
        tableA ON tableB.student_id = tableA.student_id
where   tableB.student_id= '2' 
order by tableB.assignment_id desc 
limit 1

INNER JOINs are used to return data where the data is in both tables (so en entry would exist in tableA and tableB).

LEFT JOINs are used when you wish to retrieve all data from tableA and those values that are available in tableB.

So, lets say you had

TABLEA
-------
1
2

and

TABLEB
-------
1

SELECT *
FROM TABLEA INNER JOIN
TABLEB ON TABLEA.ID = TABLEB.ID

would return

1,1

Whereas

SELECT *
FROM TABLEA LEFT JOIN
TABLEB ON TABLEA.ID = TABLEB.ID

would return

1,1
2,NULL
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Astander its working for me perfectly and thanks for explaining nicely
1

You need to use join

SELECT a.student_id,
       b.Assignment_id,
       a.email,
       b.last_submitted
FROM a
INNER JOIN b ON a.student_id = b.student_id
WHERE a.student_id= '2'
ORDER BY b.assignment_id DESC LIMIT 1

Comments

1

Join both the tables,

SELECT B.assignment_id,
       B.student_id,
       A.email_id ,
       B.last_submited
FROM tableB 'B',
            tableA 'A'
WHERE B.student_id= '2'
  AND A.student_id=B.student_id
ORDER BY assignment_id DESC LIMIT 1

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.