我想在“球队”表中添加新的第二列,该表代表英超联赛(英国)足球排名。目前,该表仅包含每个足球队的名称。
该列将称为“已参加”,它将列出每个团队参加的比赛次数。我想从一个称为“游戏”的单独表中计算此数字(整数数据类型),该表记录了游戏装置的历史记录。这可能包括使用SQL的本机“ COUNT”功能。
我试图使用一个函数来帮助我完成此任务,但目前它会将所有值插入为“ 0”
CREATE FUNCTION [dbo].[GetPlayed](@Team VARCHAR)
RETURNS INT
BEGIN
RETURN(SELECT COUNT(*)
FROM games
WHERE games.Home = @Team OR games.Away = @Team);
END;
ALTER TABLE teams
ADD Played AS GetPlayed(teams.Team)
表格:
团队:
```Team
Arsenal
Bournemouth
Burnley
Chelsea
Crystal Palace
Everton
Hull City
Leicester City
Liverpool
Manchester City
Manchester United
Middlesbrough
Southampton
Stoke City
Sunderland
Swansea City
Tottenham Hotspur
Wat"For"d
West Bromwich Albion
West Ham United
```
游戏:
gameID Home HomeScore Away AwayScore GameDate
4 Arsenal 2 Chelsea 0 2018-05-26
5 Arsenal 5 Bournemouth 0 2018-04-22
6 Arsenal 1 Leicester City 1 2018-03-15
7 Bournemouth 5 Liverpool 0 2018-04-22
8 Burnley 5 Bournemouth 0 2018-04-22
9 Burnley 1 Swansea City 2 2017-11-22
10 Stoke City 0 Burnley 0 2018-01-08
11 Chelsea 1 Middlesborough 2 2017-11-22
12 Southampton 0 Chelsea 0 2018-01-01
13 Crystal Palace 1 Everton 2 2018-03-26
14 Manchester United 4 Crystal Palace 0 2018-06-01
15 Crystal Palace 0 Southampton 1 2018-04-16
16 Everton 1 Hull City 2 2017-11-20
17 Manchester City 4 Everton 0 2017-11-20
18 Hull City 0 Burnley 0 2018-06-01
19 Sunderland 2 Hull City 0 2018-06-15
20 Leicester City 3 Tottenham Hotspur 1 2017-09-20
21 Swansea City 2 Leicester City 5 2018-02-15
22 Sunderland 0 Leicester City 1 2018-01-29
23 Liverpool 3 Tottenham Hotspur 0 2018-02-28
24 Stoke City 1 Liverpool 2 2017-09-19
25 Manchester City 2 Manchester United 4 2018-05-02
26 Middlesborough 1 Southampton 1 2018-02-08
27 Stoke City 2 Middlesborough 2 2017-08-19
28 Swansea City 0 Manchester United 5 2018-06-27
29 Sunderland 1 Tottenham Hotspur 2 2017-09-01
任何帮助将不胜感激!
谢谢,罗伯特
答案 0 :(得分:0)
不带大小的VARCHAR默认为1个字符,您需要更改函数声明
CREATE FUNCTION [dbo].[GetPlayed](@Team VARCHAR(32))
.....
如果没有大小,您的参数 @Team 只会收到您传递的团队价值的第一个字母,当然,WHERE语句无法在您的游戏表中找到任何结果