简体   繁体   中英

MySql Join three tables

I have join three table into one table but there different column name but same value.

Student Table

-------------
CV_id    name
-------------
LC001     ali
LC002     ahmed
LC003     john
LC004     king

Course Table

-------------
Us_id    name
-------------
LC001     physic
LC002     maths
LC003     computer
LC004     chemistry

Bridge

-------------
sid    CV_cid
-------------
ti     LC001
ni     LC002
df     LC003
ed     LC004

Assuming you want to join by the id fields:

select s.name student_name, c.name course_name, b.sid from student s
join course c
on c.us_id = s.cv_id
join bridge b
on b.cv_id = s.cv_id

Here is some information about SQL joins

Use JOIN to achieve your result

SELECT ST.CV_id, ST.name, CO.name, BR.`sid`
FROM Student ST
INNER JOIN Course CO ON CO.Us_id = ST.CV_id
INNER JOIN Bridge BR ON BR.CV_cid = ST.CV_id

假设您将第一列的表作为主键。

SELECT t1.col, t2.col, t3.col FROM tbl1 join tbl2 ON tbl1.pk = tbl2.pk join tbl3 ON tbl2.pk = tbl3.pk

YOU CAN USE JOIN

SELECT s.CV_id, s.name, c.name, b.sid
FROM Student s
INNER JOIN Course c ON c.Us_id = s.CV_id
INNER JOIN Bridge b ON b.CV_cid = s.CV_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.

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