您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

MySQL表连接_MySQL

2025/10/16 22:46:02发布18次查看
一、内连接 语法:
select ... from join_table [inner] join join_table2 [on join_condition] where where_definition
只列出这些连接表中与连接条件相匹配的数据行。
例:
select * from table1 a join table2 b on a.dept_id = b.id;或select * from table1 a, table2 b where a.dept_id = b.id;
二、外连接 语法:
select ... from join_table (left|right|full) [outer] join join_table2 on join_condition where where_definition
不仅列出与连接条件相匹配的行,还列出左表(左外连接)、右表(右外连接)或两个表(全外连接)中所有符合where过滤条件的数据行。需要注意的是mysql5.1暂不支持full join连接。
例:
左外连: select * from table1 a left join table2 b on a.dept_id = b.id;右外连:select * from table1 a right join table2 b on a.dept_id = b.id;
三、交叉连接 语法:
select ... from join_table cross join join_table2;
没有on子句和where子句,它返回连接表中所有数据行的笛卡尔积。
其结果集合中的数据行数等于第一个表中符合查询条件的数据行数乘以第二个表中符合查询条件的数据行数。
相当于:
select ... from table1, table2;
四、自连接 参与连接的表都是同一张表。(通过给表取别名虚拟出)
select a.name aname, b.name bname from table a left join table bon b.id = a.gmr;--查询出员工姓名和其经理的姓名 select a.name aname from table a right join table b on a.id = b.gmr; --查询出所有经理的姓名
自连接在实际应用当中比较常见,其语句也绕口,可将同一张表当成两张不同的表来理解,比如明明是同一张表,记录了员工基本信息,其中包含着每位员工的经理的id,那么则可以把它当作一张员工表,一张经理表来对待。呃~怎么说呢,还是有点绕,等到实际应用中慢慢摸索吧。。。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product