简体   繁体   中英

SQLite SQL UPDATE query based on the another table

So I have a few tables:

___ TABLE A (users info) ___
team_id | user_id | points | rewards

___ TABLE B (points for every event)___
team_id | user_id | points | rewards | event_type

___ Table C (users) ___
user_id | name | etc..

In the table AI have summary information for all the users based on the team. In the table BI have an atomic information for each event (something like a history). I would like to update information in the table A (points and rewards) by some of the same fields from table B using only user_id. My problem is that I can't understand how can I do it in one query.

For exemple I can make a query like

WITH storage as (
    SELECT
        sum(points) as points,
        sum(rewards) as rewards,
        team_id FROM B
    WHERE user_id = 1 AND team_id = 1
)

UPDATE A
SET
   points = (
      SELECT points FROM storage
),
   rewards = (
      SELECT rewards FROM storage)
WHERE user_id = 1 and team_id = 1 ;

But I would like to run this action without team_id. For example I run sql like

WITH storage as (
    SELECT
        sum(points) as points,
        sum(rewards) as rewards,
        team_id FROM B
    WHERE user_id = 1 GROUP BY team_id
)

And after that update points and rewards for each line in the table B based on the team_id. Is it possible to make a query without loop in the back-end? UPDATE: It's for the SQLite database UPDATE 2 you could find the response enter link description here

Like this?

update A set A.points = B.sumpoints, A.reward = B.sumreward 
from A, 
  (select userid, teamid, sum(points) sumpoints, sum(reward) sumreward 
   from B group by userid, teamid) B 
where A.userid = B.userid and A.teamid = B.teamid

So in the end I found the solution for the SQLite. It's really close to my first query.

WITH storage as (
    SELECT
        sum(points) as points,
        sum(rewards) as rewards,
        team_id FROM B
    WHERE team_id IS NOT NULL
    GROUP BY user_id, team_id
)

UPDATE A
SET
   points = (
      SELECT points FROM storage WHERE 
      storage.team_id = A.team_id AND storage.user_id = A.user_id
),
   rewards = (
      SELECT rewards FROM storage WHERE
      storage.team_id = A.team_id AND storage.user_id = A.user_id
)
WHERE user_id = 1;

Also it's possible to remove unused data to add where statement to the WITH storage block (add filter by user_id )

WITH storage as (
    SELECT
        sum(points) as points,
        sum(rewards) as rewards,
        team_id FROM B
    WHERE team_id IS NOT NULL AND user_id = 1
    GROUP BY team_id
)

UPDATE A
SET
   points = (
      SELECT points FROM storage WHERE storage.team_id = A.team_id
),
   rewards = (
      SELECT rewards FROM storage WHERE storage.team_id = A.team_id
)
WHERE user_id = 1;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM