美文网首页
MySQL小试牛刀

MySQL小试牛刀

作者: kangyiii | 来源:发表于2017-03-22 18:21 被阅读0次

    需求1:

    创建三个表并插入数据,比赛表(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, '阿联酋'), (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:

    现查询当前所有比赛的比赛结果:球队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;
    
    查询结果

    相关文章

      网友评论

          本文标题:MySQL小试牛刀

          本文链接:https://www.haomeiwen.com/subject/axaonttx.html