Home > database >  Get 1 or 0 IF another table value is bigger than 5
Get 1 or 0 IF another table value is bigger than 5

Time:02-01

I've been trying to do this but I can't figure it out I'm learning SQL Server.

Table: Characters

AccountID Name   ResetDay ResetWeek
--------------------------------------
Mongus   Golgon    10      15
Treas    Trical    3       10

Table: Logros

AccountID Name   ResetDay ResetWeek
--------------------------------------
Mongus   Golgon    0      0
Treas    Trical    0      0

What I need to do is IF in table Characters, column ResetDay value is bigger than 5 then Insert on table Logros, column ResetDay a 1. IF it is not bigger do nothing. It needs to match Name to make sure is giving the data to correct row.

CodePudding user response:

Basically, join these two tables on AccountID and Name, only where Characters.ResetDay > 5 and Logros.ResetDay isn't already 1.

UPDATE l
  SET l.ResetDay = 1
  FROM dbo.Logros AS l
  INNER JOIN dbo.Characters AS c
    ON l.AccountID = c.AccountID
    AND l.Name = c.Name
    WHERE c.ResetDay > 5
      AND l.ResetDay <> 1;

CodePudding user response:

For SQL Server:

UPDATE
    L
SET
    ResetDay = 1
FROM
    Logros As L
    INNER JOIN Characters As C
    ON C.AccountID = L.AccountID
    And C.Name = L.Name
WHERE
    C.ResetDay > 5
;
  •  Tags:  
  • Related