mysql与java连接查询_【java】MySQL数据库之连接查询

连接查询

首先来认识一个叫笛卡尔积 (cartesian product) 的东东,也可以叫直积。

假设我们有一个集合 A = {a, b}, 还有一个集合B = {0, 1, 2} ,那么这两个两集合的笛卡积为:{{a, 0}, {a, 1}, {a, 2}, {b, 0}, {b, 1}, {b,2}}。

就是两个集合中的元素可能存在的所有组合方式而形成的新的集合!

我是这么理解的,太具体的解释,欢迎伙伴们去百度哈!

现在我们用代码来测试演示一下:

create table one(

one_id int,

one_date char(1),

public_field int

);

insert into one values(1, 'a', 10),(2, 'b', 20),(3, 'c', 30);

ziaAfe.jpg

ziaAfe.jpg

create table two(

two_id int,

two_date char(1) not null default 't',

public_field int

);

insert into two values(2, 'B', 20),(3, 'C', 30),(4, 'D', 40);

IRnqI3.jpg

IRnqI3.jpg

select one.*, two.* from one, two;

vEFnQr.jpg

vEFnQr.jpg

这个就是表one 和 表 two 的所有元素可能出现的组合就是笛卡尔积!

内连接查询

三种语法:

select * from tableA , tableB where tabA.column=tabB.column;

select * from tableA inner join tableB on tabA.column=tabB.column;

select * from tableA cross join tableB on tabA.column=tabB.column;

内连接

只有在连接的表内数据都存在的情况下,才会做连接。内连接是把二个表连接成一个结果集,在这个结果集中仅包含那些满足条件的记录行。

select one.*,two.* from one , two where one.one_id=two.two_id;

uQZfUn.jpg

uQZfUn.jpg

select one.*,two.* from one inner join two on one.one_id=two.two_id;

nm2AJr.jpg

nm2AJr.jpg

select one.*,two.* from one cross join two on one.one_id=two.two_id;

aUNBru.jpg

aUNBru.jpg

这三种语法都可以实现内连接查询

外连接查询

如果存在不能匹配的数据,也会进行连接,不过此时 mysql 会帮我们虚拟一条不存在的记录,字段值都是为 null,帮我们完成整个连接记录。

分为:

左外连接 : left join on, left outer join on;

右外连接 :right join on, right outer join on;

语法:

select * from tableA left join tableB on tabA.column=tabB.column;

select * from tableA right join tableB on tabA.column=tabB.column;

区别在于:

左外连接 :

返回左表的所有数据,并且在右表中不能匹配的列值,其字段用空值null表示。

select one.*,two.* from one left join two on one.one_id=two.two_id;

7naUnu.jpg

7naUnu.jpg

left join – where null

返回和右表不匹配的所有数据行。

select one.*,two.* from one left join two on one.one_id=two.two_id where two.two_id is null;

FZRnma.jpg

FZRnma.jpg

right join :

右外连接 : 返回右表的所有数据,并且在左表中不能匹配的列值,其字段用空值表示。

select one.*,two.* from one right join two on one.one_id=two.two_id;

ZvaUBb.jpg

ZvaUBb.jpg

right join – where null

返回和右表不匹配的所有数据行。

select one.*,two.* from one right join two on one.one_id=two.two_id where one.one_id is null;

UNnmaa.jpg

UNnmaa.jpg

我们来看一下这个外链接在实际项目中的是如何使用的

select a.name,a.province,a.city,a.phone,b.employee_name,b.job_title from office a inner join employee b on a.office_id=b.office_id;

6RvUrq.jpg

6RvUrq.jpg

select count(b.employee_id),a.name from office a inner join employee b on a.office_id=b.office_id group by a.name;

2ammei.jpg

2ammei.jpg

select count(employee_id),office_id from employee group by office_id;

qYZjau.jpg

qYZjau.jpg

这样一对比是不是就看出我们的连接查询的用处了呢?

好的今天就分享到这里了

宝宝们回去要记得练习一下呦!

爱你们么么哒

queMJv.jpg

587108

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值