[英]How do I tell SQL to run a command using another table as the input value?
I'm trying to build a query that will show me the number of points for each team (as a start anyway) in my database. 我正在尝试建立一个查询,该查询将向我显示数据库中每个团队的得分(无论如何作为起点)。 I have two tables, one with the list of teams, and one with the matches, in the format
我有两个表格,一个是球队名单,另一个是比赛,格式如下
Week|HomeTeam|AwayTeam|HomeScore|AwayScore|Result|MatchID 周|主队|客队|主场得分|客场得分|结果|比赛ID
Where Result can be either A (away win), H (home win), or D (draw). 结果可以是A(客场获胜),H(主场获胜)或D(平局)。 I can find the number of points for a given team easily enough with
我可以很容易地找到给定团队的积分
SELECT
(SELECT COUNT(matches.Result)*3 FROM matches WHERE (HomeTeam='Arsenal' AND Result='H') OR (AwayTeam='Arsenal' AND Result='A')) +
(SELECT COUNT(matches.Result) FROM matches WHERE (HomeTeam='Arsenal' OR AwayTeam='Arsenal') AND Result='D');
I want to do is something like: 我想做的是这样的:
FOR EACH teams.Team in teams
SELECT
(SELECT COUNT(matches.Result)*3 FROM matches WHERE (HomeTeam=Team AND Result='H') OR (AwayTeam=Team AND Result='A')) +
(SELECT COUNT(matches.Result) FROM matches WHERE (HomeTeam=Team OR AwayTeam=Team) AND Result='D');
Unfortunately, this isn't working and I can't quite figure out why. 不幸的是,这是行不通的,我也不知道为什么。 What am I doing wrong?
我究竟做错了什么?
Simple use a subquery: 简单使用子查询:
SELECT teams.Team,
((SELECT COUNT(matches.Result)*3 FROM matches WHERE (HomeTeam=teams.Team AND Result='H') OR (AwayTeam=Team AND Result='A')) +
(SELECT COUNT(matches.Result) FROM matches WHERE (HomeTeam=teams.Team OR AwayTeam=teams.Team) AND Result='D')) as Points
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.