多表查询关联查询sql语句(最多可三表关联)实例讲解
select l.limitid, l.limitname, l.limiturl, m.modelname,o.operatename from limits l left join model m on m.modelid=l.limitmodelid left join operate o on o.operateid=l.limitoperatenameid;
用户表user:
user_id | user_name | user_sex |
100111 | 佳佳 | 女 |
课程表subject:
subject_id | subject_name | achievement | user_number |
111 | 计算机科学与技术 | 99 | 100111 |
学院表college:
college_id | college_name | subject_number |
201 | 网络工程学院 | 111 |
select
u.user_id, u.user_name,u.user_sex,c.college_name,s.subject_name, s.achievement
from user u
left join subject s on s.user_number=u. user_id
left join college c on c.subject_number=s.subject_id;
三表关联查询结果:
user_id(学号) | user_name(姓名) | user_sex(性别) | college_name(学院) | subject_name(学科) | achievement(成绩) |
100111 | 佳佳 | 女 | 网络工程学院 | 计算机科学与技术 | 99 |