mysql数据库连表查询的几种方法

来源:这里教程网 时间:2026-03-01 15:15:30 作者:

  1.首先介绍表连接分类(内连接,外连接,交叉连接)和连接方法(如下):  A)内连接:join,inner join  B)外连接:left join,left outer join,right join,right outer join,union  C)交叉连接:cross join  2.内连接  查找两个表中ID相同的数据,查询结果会拼成一个表格,输出两个表中id都相同  select a.*,b.* from tableA a, tableB b where a.id=b.userid  或者使用如下语句  select a.*,b.* from tableA a inner join tableB b on a.id=b.userid  3.外链接  外链接包括左链接和右链接 left join ,right join; 以关键字left(right)为参照物,用其左(右)为主表,此时必须满足on后面的查询条件的同时并且输出主表中的所有数据,(即使该数据不对应从表中的数据)  左外链接    郑州看胎记费用 http://m.zykdtj.com/  select a.*,b.* from tableA a left join tableB b on a.id=b.userid  右外链接  select a.*,b.* from tableA a right join table B on a.id=b.userid  全外连接:  该外连接在MySQL数据库中不支持:其作用就是不仅满足on后面的条件,还要将两个表中不匹配的数据输出  3.交叉链接  select a.*,b.* from tableA a cross join tableB b  左表(“cross join”关键字左边的表)中的每一行与右表(“cross join”关键字右边的表)中的所有行组合,交叉联接的结果是一个笛卡尔积。此时的数据匹配种类过于多样化,一般来讲:该方法的作用对一些需要通过对应ID寻找数据库信息的需求十分渺小

相关推荐