Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
基于另一个表的SQLite SQL更新查询_Sql_Sqlite - Fatal编程技术网

基于另一个表的SQLite SQL更新查询

基于另一个表的SQLite SQL更新查询,sql,sqlite,Sql,Sqlite,所以我有几张桌子: ___ 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.. 在表A中,我有基于团队的所有用户的摘要信息。在表B中,我有每个事件的

所以我有几张桌子:

___ 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..
在表A中,我有基于团队的所有用户的摘要信息。在表B中,我有每个事件的原子信息(类似于历史)。我想通过表B中的一些相同字段仅使用用户id来更新表A(积分和奖励)中的信息。我的问题是我不明白如何在一个查询中完成

例如,我可以做如下查询

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 ;
但是我希望在没有团队id的情况下运行此操作

WITH storage as (
    SELECT
        sum(points) as points,
        sum(rewards) as rewards,
        team_id FROM B
    WHERE user_id = 1 GROUP BY team_id
)
然后根据团队id更新表B中每一行的积分和奖励。 是否可以在后端不使用循环进行查询? 更新: 它是用于SQLite数据库的 更新2 你能找到这样的回答吗

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

所以最后我找到了SQLite的解决方案。这非常接近我的第一个问题

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;
此外,还可以删除未使用的数据以将where语句添加到WITH存储块(按用户id添加筛选器)


谢谢您的回答,sqlite语法在更新查询中没有关键字FROM
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;