delphi 如何使用主键从数据库中提取数据

ufj5ltwl  于 2022-11-04  发布在  其他
关注(0)|答案(1)|浏览(190)

我试图从我的数据库中提取一个人的名字,该数据库包含两个表,其中只有ID(主键)。
我正在努力想出一个解决方案,虽然我确实有笔记,我已经写了,逻辑似乎检查出来(至少对我来说)。

if P1Score > P2Score
then winner := P1ID 
else winner := P2ID 

winner in tblGames = the ID of winner in tblPlayers

WinnerName := first name of Winner + surname of Winner in tblPlayers

这就是我的逻辑,很明显它遗漏了很多,但我似乎无法进一步展开

我从tblGames中得到了这个人的ID,但是现在我很难理解如何使用这个ID从我的tblPlayers中提取Name和Surname,并将其赋给一个变量,这样我就可以将其放入tblGames的赢家Column中。
我已经尝试了一些事情使用我自己的思想过程,但我不知道足够的 Delphi 和数据库,实际上正确地实现它。

BEGIN
    if (StrToInt(P1_score) - StrToInt(P2_score) = 0) then
    Draw := True
    else
    Draw:= False;

    if StrtoInt(P1_Score) > StrToInt(P2_Score) then
        winnerID := P1_ID
        else
        winnerID := P2_ID;

 with dmTournament do
      begin

    tblGames.Insert;
    tblGames['Player1_Id'] := StrToInt(P1_ID);
    tblGames['Player2_ID'] := StrToInt(P2_ID);
    tblGames['Player1_score'] := StrToInt(P1_Score);
    tblGames['Player2_Score'] := StrToInt(P2_Score);
    tblGames['Draw'] := Draw;
    tblGames['Winner'] := WinnerName;  //How do I get WinnerName(?) 
    tblGames.Post;
      end;
  END;
wbrvyc0a

wbrvyc0a1#

您可以按相同顺序执行以下查询以完成任务。
将所有游戏设为平局:

UPDATE Games SET draw = true

当玩家1赢家时更新:

UPDATE Games, Players SET  
  Games.draw = false,
  Games.winner = CONCAT(Players.first_name, ' ', Players.last_name)
WHERE
  (Games.player1_score > Games.player2_score) AND (Games.player1_id=Players.ID)

当玩家2赢家时更新:

UPDATE Games, Players SET  
  Games.draw = false,
  Games.winner = CONCAT(Players.first_name, ' ', Players.last_name)
WHERE
  (Games.player2_score > Games.player1_score) AND (Games.player2_id=Players.ID)

相关问题