mysql面试题左链接右_mysql左连接查询面试题

dbc62dbeac5be29f9c42aebf402ea433.png

[/img]

第一步:首先创建第一个表,并添加数据,如下图:

create table m(

mid int primary key auto_increment,

hid int,

gid int,

result varchar(50),

mtime date

);

insert into m

(hid,gid,result,mtime)

values

(1,2,'2:0','2006-05-21'),

(2,3,'1:2','2006-06-21'),

(3,1,'2:5','2006-05-25'),

(2,1,'3:2','2006-07-21');

[img]

b21d35b64e9eb8877bb3f40304b3e8ab.png

[/img]

第二步:创建第二张表,并模拟添加数据,如下:

create table t(

tid int,

tname varchar(10)

);

insert into t

values

(1,'巴萨'),

(2,'皇马'),

(3,'野马队');

[img]

8acd21ee9de07034c9aa3b447062043c.png

[/img]

第三步,先把输出的顺序搞定,如下:

select hid, result, gid,mtime from m;

[img]

6f94f3dbf2cd0ee07f559f5ceb7916ec.png

[/img]

第四步:把主队的名字取出来,如下:

select hid,tname, result,gid,mtime

from

m left join t

on m.hid = t.tid;

[img]

7c1b42a6e77732ce8c40e5025cce58db.png

[/img]

第五步:把客队的名字也取出来,如下:

select hid,t1.tname as '主队', result,gid,t2.tname as '客队',mtime

from

m left join t as t1

on m.hid = t1.tid

left join t as t2

on m.gid = t2.tid;

[img]

f69d8212fb71e7494eac5b3249083c5c.png

[/img]

第六步:再查询一次,这次不需要 hid 与 gid 这两列, ok let's go

select t1.tname as '主队',result as '比分',t2.tname as '客队',mtime as '比赛时间'

from

m left join t as t1

on m.hid = t1.tid

left join t as t2

on m.gid = t2.tid;

[img]

2f88e09c82115377f62bb2cd45cdbf86.png

[/img]

第七步:添加查询条件:查询2006-06-01  到2006-07-01之间的比赛

select t1.tname as '主队',result as '比分',t2.tname as '客队',mtime as '比赛时间'

from

m left join t as t1

on m.hid = t1.tid

left join t as t2

on m.gid = t2.tid

where mtime between '2006-06-01' and '2006-07-01';

[img]

b5cefca312014cbb753566d60d1d8409.png

[/img]

over

daf1fd7edd3ad89da27090cd90ec8014.png

大小: 2.6 KB

52a1825d85c72b4a2afd68369e3e0412.png

大小: 1.3 KB

105795695ac69e5b321cd7b4cb0256c6.png

大小: 3.1 KB

530a26a50469bf815b907ae7d6722783.png

大小: 4.2 KB

5649c0ceac679c47a1c3bb93ab8e99dc.png

大小: 6.2 KB

17fea110d3972f10ebff2771cdb0b8bb.png

大小: 7.1 KB

4f2599ba940cd8e268a3f2ff6393ac1c.png

大小: 5.9 KB

8fa063a194b74449f5c5ab9dd4e9e46b.png

大小: 93.9 KB

0

0

分享到:

18e900b8666ce6f233d25ec02f95ee59.png

72dd548719f0ace4d5f9bca64e1d7715.png

2013-02-28 13:20

浏览 1016

评论

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值