我工作的一個項目(基於點)投注程序的值,如果需要SQL Server上創建一個存儲過程,執行以下操作:的Sql - 遍歷兩個表,總結根據病情
- 如果用戶在團隊中投注並獲得正確結果,則總計3分;
- 總和1分,如果結果與用戶猜測的結果不同,但仍設法正確判斷球隊是否贏,輸或關聯 (即用戶對皇馬 - 巴塞羅那投注;用戶猜測2 - 1;結果1 - 0);
- 如果用戶得到任何錯誤,他不會得到任何分數。
我一直在尋找一種方式來做到這一點,但我無法找出一種方法。 這可能是一個不好的問題,我們都必須從某個地方開始。 我想過循環,這在理論上是我需要做的,但我需要遍歷賭注表中的每一行,並將其值與遊戲表中每行的值進行比較。
如果你能幫助我,那會很棒。謝謝。
編輯:哦,拍攝。對不起,我正要添加表格,而我忘記了。那麼,在這裏,他們是:
-Bets表:
CREATE TABLE Bets(
id_bet NUMERIC(18,0) NOT NULL PRIMARY KEY
,id_user NUMERIC(18,0)
,id_game NUMERIC(18,0)
,date NUMERIC(8,0)
,time NUMERIC(4,0)
,goals_home NUMERIC(18,0)
,goals_visitor NUMERIC(18,0)
);
INSERT INTO Bets(id_bet,id_user,id_game,date,time,goals_home,goals_visitor) VALUES (1,1,4,20170614,1600,1,1);
INSERT INTO Bets(id_bet,id_user,id_game,date,time,goals_home,goals_visitor) VALUES (2,1,3,20170614,1600,1,1);
INSERT INTO Bets(id_bet,id_user,id_game,date,time,goals_home,goals_visitor) VALUES (3,7,3,20170614,1600,1,1);
INSERT INTO Bets(id_bet,id_user,id_game,date,time,goals_home,goals_visitor) VALUES (4,7,4,20170614,1600,1,1);
-games表:
CREATE TABLE Games(
id_game NUMERIC(18,0) NOT NULL PRIMARY KEY
,num_game NUMERIC(18,0)
,id_club_home NUMERIC(18,0)
,id_club_visitor NUMERIC(18,0)
,id_competition NUMERIC(18,0)
,goals_home NUMERIC(18,0)
,goals_visitor NUMERIC(18,0)
,date NUMERIC(8,0)
,time NUMERIC(4,0)
);
INSERT INTO Games(id_game,num_game,id_club_home,id_club_visitor,id_competition,goals_home,goals_visitor,date,time) VALUES (1,1,3,6,2,2,2,20170614,1700);
INSERT INTO Games(id_game,num_game,id_club_home,id_club_visitor,id_competition,goals_home,goals_visitor,date,time) VALUES (3,2,4,3,2,1,3,20170614,1800);
INSERT INTO Games(id_game,num_game,id_club_home,id_club_visitor,id_competition,goals_home,goals_visitor,date,time) VALUES (4,3,3,4,2,1,3,20170614,1800);
INSERT INTO Games(id_game,num_game,id_club_home,id_club_visitor,id_competition,goals_home,goals_visitor,date,time) VALUES (5,4,6,3,2,2,3,20170614,1800);
INSERT INTO Games(id_game,num_game,id_club_home,id_club_visitor,id_competition,goals_home,goals_visitor,date,time) VALUES (6,5,4,6,2,NULL,NULL,20170614,1600);
-I've想這(和其他的方法,但沒不保存它們):
CREATE PROCEDURE [dbo].[Count_Points] @valor AS INT OUTPUT
AS
BEGIN
SET NOCOUNT ON;
DECLARE @a AS INT
DECLARE @goals_home_bet AS NUMERIC(18, 0) = (
SELECT Bets.goals_home
FROM Bets
INNER JOIN Games ON Bets.id_game = Games.id_game
)
DECLARE @goals_visitor_bet AS NUMERIC(18, 0) = (
SELECT Bets.goals_visitor
FROM Bets
INNER JOIN Games ON Bets.id_game = Games.id_game
)
DECLARE @goals_home_games AS NUMERIC(18, 0) = (
SELECT Games.goals_home
FROM Bets
INNER JOIN Games ON Bets.id_game = Games.id_game
)
DECLARE @goals_visitor_games AS NUMERIC(18, 0) = (
SELECT Games.goals_visitor
FROM Bets
INNER JOIN Games ON Bets.id_game = Games.id_joid_gamego
)
SET @valor = 0
SET @a = 1
SET @valor = sum(@valor + 3)
WHILE (
(
SELECT max(id_bet)
FROM Bets
) > @a
)
BEGIN
SET @a += 1
END
END
它只是用於測試它是否工作(因此僅引用值3),但顯然我只能得到3的值。其他方式我也會得到3或者甚至是。
編輯2:我的解決方案(可能不是最有效的,但仍然):從bit
CREATE PROCEDURE [dbo].[Calculate_Points]
@control as int output,
@points as int output,
@nickname varchar(50)
AS
BEGIN
SET NOCOUNT ON;
declare @cont numeric(18,0)=1
declare @cont1 numeric(18,0)
declare @id_user numeric(18,0)=(select num_user from Users where [email protected])
declare @id_game numeric(18,0)
declare @goals_home_bet numeric(18,0)
declare @goals_visitor_bet numeric(18,0)
declare @goals_home_game numeric(18,0)
declare @goals_visitor_game numeric(18,0)
set @points=0
while(@cont<=(select max(num_bet) from Bets))
begin
if exists (select id_user from Bets where @id_user=id_user and [email protected])
begin
set @id_game=(select id_game from Bets where [email protected])
set @goals_home_bet=(select goals_home from Bets where [email protected])
set @goals_visitor_bet=(select goals_visitor from Bets where [email protected])
set @cont1=1
while(@cont1<(select max(id_game) from Games))
begin
if(@id_game=(select id_game from Games where id_game [email protected]))
begin
set @goals_home_game=(select goals_home from Games where [email protected])
set @goals_visitor_game=(select goals_visitor from Games where [email protected])
if(@[email protected]_casa_jogo and @[email protected]_fora_jogo)
set @pontos+=3
else if((@goals_home_bet>@goals_visitor_bet and @goals_home_game>@goals_visitor_game) or (@goals_home_bet<@goals_visitor_bet and @goals_home_game<@goals_visitor_bet) or (@[email protected]_visitor_bet and @[email protected]_visitor_bet))
set @points+=1
end
set @cont1+=1
end
end
set @cont+=1
end
END
顯示您的表格模式以及目前爲止嘗試查詢的內容。樣本數據和結果始終是一個優點。 –
這裏是一個開始的好地方。 http://spaghettidba.com/2015/04/24/how-to-post-a-t-sql-question-on-a-public-forum/ –
對不起,忘了添加信息。更新! – taiko