mysql inner join left join ,right join区别
2014-03-07 20:11
543 查看
inner join 内连接,left join 左连接,right join右连接;
先来看看inner join
会发现,得出的每一条数据都不会有空值 。
再来看看左连接:
select * from Persons left join Orders on Persons.Id_P = Orders.Id_P;
会发现,右边会出现空值 ,也就是说左边的为准,左边是指persons表,你会在左连接的表中找到Persons表的三条完整的数据 ,如果在Orders表中没有数据,则用null来补全。同理,右连接也是这个道理。
select * from Persons right join Orders on Persons.Id_P = Orders.Id_P;
以右边的Orders的5条数据为准,可以在右连接的表中完完找出Orders的5条数据,如果左边对应不上则用null值来比较。
细看上面的语句,找出Persons.Id_P 大于 Orders.Id_P的数据。首先先从Persons的第一条数据开始找,然后对就Orders表,看看有哪一条是数据是比Persons的Id_p少的,然后挑选出来,其实就两个foreach循环。按照上面的原理,你可以试试左右连接的> 和 <;
其实内连接就是左右连接的集合。下面来做一下下面的例子:
有一张表是用来记录每天的收入table rmb,要求计算出当天以上的总收入。
DROP TABLE IF EXISTS `rmb`;
CREATE TABLE `rmb` (
`date` char(8) NOT NULL,
`rmb` int(11) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
select tmp1.`date`, tmp1.rmb, sum(tmp2.rmb) total from rmb as tmp1
left join rmb as tmp2
on tmp1.`date` >= tmp2.`date` group by tmp1.`date`;
create table if not exists `Match`(
matchID int not null primary key auto_increment,
hostTeamID int not null,
guestTeamID int not null,
matchResult varchar(20) not null,
matchTime datetime not null
)charset utf8;
create table Team(
temaID int not null primary key auto_increment,
temaName int not null
)charset utf8;
# 要求 Match的hostTeamID 与 guestTeamID 都
# 与 Team中的teamID关联 查出
# 2014-03-01 到 2014-03-31之间举行的所有比赛,关且
# 用以下形式列出
# 拜仁 2 : 0 不来梅 2014-03-21
# 思路:
# 先选出主队的队名
# 结果
# 再取出客队的名称
主要是采用三表查询:
select team.teamName, Matchresult, tmp.teamName, matchTime from `match`
left join team
on hostTeamID = team.TeamID
left join
team as tmp
on guestTeamID = tmp.TeamID where MatchTime between '2014-03-01' and '2014-03-31';
create table if not exists `Persons` ( Id_P smallint not null, LastName varchar(25) not null, FirstName varchar(50) not null, Address varchar(100) not null, City varchar(25) not null )charset utf8; insert into Persons values ('1', 'Adams', 'John', 'Oxford Street', 'London'), ('2', 'Bush', 'George', 'Fifth Avenue', 'New York'), ('3', 'Carter', 'Thomas', 'Changan Street', 'Beijing'); create table if not exists Orders( Id_O smallint not null, OrderNO int not null, Id_P smallint not null )charset utf8; insert into Orders values (1, 77895, 3), (2, 44678, 3), (3, 22456, 1), (4, 24256, 1), (5, 34764, 65);
先来看看inner join
select * from Persons inner join Orders on Persons.Id_P = Orders.Id_P;
会发现,得出的每一条数据都不会有空值 。
再来看看左连接:
select * from Persons left join Orders on Persons.Id_P = Orders.Id_P;
会发现,右边会出现空值 ,也就是说左边的为准,左边是指persons表,你会在左连接的表中找到Persons表的三条完整的数据 ,如果在Orders表中没有数据,则用null来补全。同理,右连接也是这个道理。
select * from Persons right join Orders on Persons.Id_P = Orders.Id_P;
以右边的Orders的5条数据为准,可以在右连接的表中完完找出Orders的5条数据,如果左边对应不上则用null值来比较。
select * from Persons inner join Orders on Persons.Id_P > Orders.Id_P;
细看上面的语句,找出Persons.Id_P 大于 Orders.Id_P的数据。首先先从Persons的第一条数据开始找,然后对就Orders表,看看有哪一条是数据是比Persons的Id_p少的,然后挑选出来,其实就两个foreach循环。按照上面的原理,你可以试试左右连接的> 和 <;
其实内连接就是左右连接的集合。下面来做一下下面的例子:
有一张表是用来记录每天的收入table rmb,要求计算出当天以上的总收入。
DROP TABLE IF EXISTS `rmb`;
CREATE TABLE `rmb` (
`date` char(8) NOT NULL,
`rmb` int(11) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
INSERT INTO `rmb` VALUES ('20140301',100),('20140302',50),('20140309',500);#思路:首先进行自己连接查询:条件是小于或等于当天的日期,再以分组求和
select tmp1.`date`, tmp1.rmb, sum(tmp2.rmb) total from rmb as tmp1
left join rmb as tmp2
on tmp1.`date` >= tmp2.`date` group by tmp1.`date`;
create table if not exists `Match`(
matchID int not null primary key auto_increment,
hostTeamID int not null,
guestTeamID int not null,
matchResult varchar(20) not null,
matchTime datetime not null
)charset utf8;
create table Team(
temaID int not null primary key auto_increment,
temaName int not null
)charset utf8;
# 要求 Match的hostTeamID 与 guestTeamID 都
# 与 Team中的teamID关联 查出
# 2014-03-01 到 2014-03-31之间举行的所有比赛,关且
# 用以下形式列出
# 拜仁 2 : 0 不来梅 2014-03-21
# 思路:
# 先选出主队的队名
# 结果
# 再取出客队的名称
主要是采用三表查询:
select team.teamName, Matchresult, tmp.teamName, matchTime from `match`
left join team
on hostTeamID = team.TeamID
left join
team as tmp
on guestTeamID = tmp.TeamID where MatchTime between '2014-03-01' and '2014-03-31';
相关文章推荐
- Mysql多表表关联查询 inner Join left join right join
- mysql多表连接查询inner/left/right/full/cross join
- mysql连表查询之left join on / right join on / inner&nb
- two articles about MySQL - LEFT JOIN and RIGHT JOIN, INNER JOIN and OUTER JOIN
- mysql left join right join inner join
- MySQL left join right join inner join 区别
- mysql中left join right join inner join用法
- MySQL - LEFT JOIN and RIGHT JOIN, INNER JOIN and OUTER JOIN
- left join/right join/inner join操作演示
- 超详细mysql left join,right join,inner join用法分析
- SQL语句:left join/right join/inner join/ 用法比较(转)
- 关于mysql inner join 连接查询的优化
- sql语法:inner join on, left join on, right join on详细使用方法
- Inner join and Left Outer Joins in LINQ with Entity Framework
- MySQL Left join 或 right join 时返回 null字段 给默认值
- sql语法:inner join on, left join on, right join on详细使用方法
- sql语法:inner join on, left join on, right join on具体用法
- sql语法:inner join on, left join on, right join on详细使用方法
- inner join on, left join on, right join on
- 超详细mysql left join,right join,inner join用法分析