需求1:
創(chuàng)建三個表并插入數(shù)據(jù)勿决,比賽表(match)臼膏,球隊表(team)参淫,運動員表(player)
比賽表(match)
比賽ID(主鍵):m_id int
球隊一ID:t1_id int
球隊二ID:t2_id int
球隊一進球:t1_score int
球隊二進球:t2_score int
比賽時間:m_time int球隊表(team)
球隊ID(主鍵):t_id int
球隊名:t_name varchar運動員表(player)
運動員ID(主鍵):p_id int
運動員姓名:p_name varchar
球隊ID:t_id int
代碼如下:
create table `match` (
m_id int unsigned primary key auto_increment,
t1_id int unsigned comment '球隊一ID',
t2_id int unsigned comment '球隊二ID',
t1_score int comment '球隊一進球',
t2_score int comment '球隊二進球',
m_time int comment '比賽時間 時間戳'
)charset=utf8;
insert into `match` values
(null, 3, 4, 1, 2, unix_timestamp('2015-01-31 17:00:00')),
(null, 1, 2, 2, 3, unix_timestamp('2015-01-30 17:00:00')),
(null, 4, 2, 2, 0, unix_timestamp('2015-01-27 17:00:00')),
(null, 3, 1, 2, 0, unix_timestamp('2015-01-26 17:00:00')),
(null, 5, 4, 0, 2, unix_timestamp('2015-01-22 18:30:00')),
(null, 8, 5, 0, 1, unix_timestamp('2015-01-10 17:00:00')),
(null, 5, 7, 2, 1, unix_timestamp('2015-01-14 17:00:00')),
(null, 5, 6, 2, 1, unix_timestamp('2015-01-18 17:00:00'));
-- 球隊
create table `team` (
t_id int unsigned primary key auto_increment,
t_name varchar(20)
)charset=utf8;
insert into `team` values
(1, '伊拉克'), (2, '阿聯(lián)酋'), (3, '韓國'), (4, '澳大利亞'), (5, '中國'), (6, '朝鮮'), (7, '烏茲別克斯坦'), (8, '沙特');
-- 運動員
create table `player` (
p_id int unsigned primary key auto_increment,
p_name varchar(20),
t_id int unsigned comment '球隊ID'
)charset=utf8;
insert into `player` values
(null, '張琳芃', 5),(null, '郜林', 5),(null, '孫可', 5),(null, '王大雷', 5),(null, '吳曦', 5) ,(null, '于海', 5);
結果展示:
需求2:
現(xiàn)查詢當前所有比賽的比賽結果:球隊1 球隊1得分 球隊2得分 球隊2 時間(例如:伊拉克 1 2 澳大利亞 1422694800)
select t1.t_name as t1_name, m.t1_score, m.t2_score, t2.t_name as t2_name, m.m_time from `match` as m
left join `team` as t1 ON
m.t1_id = t1.t_id
left join `team` as t2 ON
m.t2_id=t2.t_id;