【问题标题】:Set value on table if related table value in array (postgresql)如果数组中的相关表值(postgresql),则在表上设置值
【发布时间】:2018-11-25 22:17:37
【问题描述】:

我有 2 张桌子,usersprofiles。如果users.emails 在列表('email1','email2',...) 中,我想将profiles.verified 设置为true

updating table rows in postgres using subquery 等其他 SO 线程的启发,我一直在尝试做类似的事情,

UPDATE 
  profiles p1  
SET 
  verified = true
FROM 
  profiles p2
  INNER JOIN users u1 on u1.id = p2.user_id
WHERE 
  u1.email in ('email1','email2',...)

但它只是将profiles 中的所有记录的profiles.verified 更新为true

如果profile 记录与带有指定列表中的电子邮件的users 记录相关,我如何才能只更新它们?

【问题讨论】:

    标签: sql postgresql join sql-update


    【解决方案1】:

    在 Postgres 中,您没有提到表被更新两次:

    UPDATE profiles p1  
        SET verified = true
    FROM users u1 
    WHERE u1.id = p1.user_id AND
          u1.email in ('email1', 'email2', ...)
    

    【讨论】:

      猜你喜欢
      • 2021-09-09
      • 1970-01-01
      • 2014-09-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-23
      • 2014-03-22
      相关资源
      最近更新 更多