Say I have table student, and table advisor. student contains ID and name:
+----+------+
| ID | name |
+----+------+
advisor contains s_id and i_id:
+------+------+
| s_id | i_id |
+------+------+
how to join with result like this:
+----+------+------+
| ID | name | i_id |
+----+------+------+
I've tried
select *
from student join advisor on student.ID=advisor.s_id
But it returns
+----+------+------+------+
| ID | name | i_id | s_id |
+----+------+------+------+
Thanks!
Modify your existing query by including in the select list only the columns you actually want to see:
SELECT s.ID, s.name, a.i_id
FROM student s
INNER JOIN advisor a
on s.ID = a.s_id;
In case a given student might actually have no advisors, you could use a left join here to report NA
for that student:
SELECT s.ID, s.name, COALESCE(a.i_id, 'NA') AS i_id
FROM student s
LEFT JOIN advisor a
on s.ID = a.s_id;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.