码迷,mamicode.com
首页 > 数据库 > 详细

mysql自关联

时间:2018-12-05 20:29:15      阅读:242      评论:0      收藏:0      [点我收藏+]

标签:nbsp   HERE   mysql   sele   数据   course   left join   span   mys   

自关联操作       
 多表连接查询 inner  join 内查询   left  join  左查询   right  join  右查询                                                                                                         
 select student.id,student.name,classes.name from student inner join classes on student.cls_id = classes.id;                                                                  inner join内连 left join左连 right join右连     表名join 表名 on 连接字段        
                      
 select student.name,course_student.cid from student left join course_student on student.id = course_student.sid;                                                              以student.id与course_student.sid连接匹配,打印student.name,course_student.cid         
                       
    select student.name,course.name from student left join course_student on student.id = course_student.sid left join course on course_student.cid = course.id;                                                                    三表(2明细表+1关系表)相连取交集打印学生名字与课程名字       
                       
select * from areas as p inner join areas as a on p.aid = a.pid where p.atitle = ‘河南省‘;                                                                      省、市,两表查询在一张表里(as起别名)用上级id打印,                                    
 
 select * from areas as p inner join areas as c on c.pid = p.aid inner join areas as a on a.pid = c.aid where a.atitle = ‘二七区‘;                                                                  省、市、区,三表查询在一张表里(as起别名)用上级id打印区表title为‘二七区’的数据     
 
select * from from areas as p inner join areas as c on c.pid = p.aid inner join areas as  as a on a.pid = c.aid where p.atitle = ‘河南省‘;        打印河南省所有的市和所有的区
 

mysql自关联

标签:nbsp   HERE   mysql   sele   数据   course   left join   span   mys   

原文地址:https://www.cnblogs.com/sunzhiqi/p/10072700.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!