mysql 内连接,连续两次使用同一张表,自连接

来源:互联网 发布:淘宝店铺可以改类目吗 编辑:程序博客网 时间:2024/05/16 10:33

1、内连接


有两个表,学生表student,班级表:class

班级表class:



学生表student:



内连接:

select * from class INNER JOIN student on class.id=student.classId



自己也喜欢写成如下这种形式:


select * from class,student where class.id=student.classId





2、连续两次使用同一张表

有两张表:material原料表,单位表unit:

material表: pUnit为采购单位,sUnit为库存单位



单位表unit:



在这里,查询material的信息时,我们需要两次关联单位表unit

select *from material m INNER JOIN unit u on m.pUnit=u.id 

INNER JOIN unit d on m.sUnit=d.id



3、自连接,自关联


在做城市表的时候,城市和省份用的自关联,查询出城市且查出该城市所属的省份

表city:



select * from city c INNER JOIN city u on c.pid=u.id where c.pid=1










原创粉丝点击