如何生成SQL查询

时间:2017-08-02 20:14:06

标签: mysql sql

我有一个关于足球(足球)统计数据的数据库。其中一个表是匹配,其中包含以下行:

  • ID
  • match_date
  • HOME_TEAM
  • AWAY_TEAM
  • home_level
  • away_level
  • home_score
  • 客场得分

另一个表格是目标,其中包含以下行:

  • ID
  • match_id
  • goal_minute
  • HOME_TEAM
  • AWAY_TEAM
  • home_level
  • away_level
  • goal_team
  • concede_team
  • goal_type_id
  • attack_type_id

当home_level为3 away_level为1 (away_score - home_score> 2)时,我想选择所有目标。

如何生成此查询,包括2个表?

1 个答案:

答案 0 :(得分:-1)

试试这个:

select  * 
from    matches m, 
        goal    g 
where   m.id = g.match_id 
and     g.home_level = 3 
and     g.away_level = 1 
and     m.away_score - m.home_score > 2

select  * 
from     matches m, 
         goal    g 
where    m.id = g.match_id 
and      g.home_level = 3 
and      g.away_level = 1 
and      (m.away_score - m.home_score) > 2

select     * 
from       matches m 
INNER_JOIN goal    g  ON  m.id = g.match_id 
                      and g.home_level = 3 
                      and g.away_level = 1 
                      and (m.away_score - m.home_score) > 2