-1

I have 4 different tables:

student: sid, name, gender 

grades: sid, grade

address: postcode, sid, distance_to_school

health_condition: sid, health_code

Now I want to make a new table that contains all of the columns that I mentioned above. I need to join them based on the sid. I tried to add columns separately but the code is really long. So is there another way to do this task?

SadSalad
  • 69
  • 2
  • 12

3 Answers3

0

If you want to show the data from the fifth table that you want to create, I think with the query is better, so you don't need the fifth table. Just

"SELECT * FROM student s
INNER JOIN grades g
on s.sid = g.sid
INNER JOIN address a
on s.sid = a.sid
INNER JOIN health_condition h
on s.sid = h.sid
ORDER BY sid ASC";
0

Please use below query will give you the exact result set.

SELECT t1.sid, t1.name, t1.gender , t2.grade,t3.postcode, t3.distance_to_school, 
t4.health_code AS 
student as t1 inner join grades t2 on t1.sid=t2.sid 
inner join address as t3 on t3.sid= t1.sid 
inner join health_condition as t4 on t4.sid=t1.sid 
0

Try this query

FROM student s
LEFT JOIN grades g on g.sid = s.sid
LEFT JOIN address a on a.sid = s.sid
LEFT JOIN health_condition h on h.sid = s.sid
WHERE s.sid = ;
krr2020
  • 23
  • 1
  • 7