SQL工作改进

时间:2013-05-02 17:05:31

标签: mysql sql

玩家表:

player_ID
player_name
position_id

职位表:

position_ID
position_description

团队表:

TEAM_ID
TEAM_NAME

游戏桌:

Game_id
home_team_id
guest_team_id
score
location
date

统计表

game_id
player_id
number of fouls
number of rebounds

我尝试了以下查询来查找玩家可以在其中玩的各种位置:

select  player.player_name,position.position_name
from player,position
where player.fname='John' and position_name='Guard,Midfield,Striker'

我已经尝试过以下查询来列出游戏日期和位置,例如5次犯规,我尝试了这个查询:

select game.date,location,number of fouls, 
from game,stats
where game.date=game.id
 and game.location=game.id
 and number of fouls > 5
group by game.date,game.location

1 个答案:

答案 0 :(得分:0)

至少这是一个开始,但首先要说几点:

  1. 我会避免使用空格的列名。如果你使用反引号,MySQL会处理它们,但我发现它们比它们的价值更麻烦。

  2. 这些查询并不是非常困难。如果你有机会,花一个小时左右做一个基本的MySQL教程。 MySQL site有一些例子。

  3. 当您从两个或更多表中选择时,您几乎总是希望JOIN表。教程将涵盖这一点。

  4. 好的,这是查询...

    他们可以玩的球员和位置:

    SELECT
      Player.Player_ID,
      Player.Player_Name,
      Position.Position_Description
    FROM Player
    INNER JOIN Position ON Player.Position_ID = Position.Position_ID
    WHERE Player.FName = 'John'
      AND Position_Name IN ('Guard', 'Midfield', 'Striker')
    

    超过5次犯规的比赛:

    SELECT
      Game.Date,
      Game.Location,
      SUM(Stats.`number of fouls`) AS TotalFouls
    FROM Game
    INNER JOIN Stats ON Game.Game_ID = Stats.Game_ID
    GROUP BY Game.Date, Game.Location
    HAVING SUM(Stats.`number of fouls`) > 5