sql - Postgres Inner join insert into 和 select from 语法

标签 sql postgresql inner-join

我正在尝试使用节点和 typescript 创建一个忘记密码的界面。我想将忘记密码 token 插入忘记密码数据库中,我正在使用以下查询来完成此操作。基本上我使用 guid 作为标记并尝试引用我的人员、派对和 contact_details 表

INSERT INTO 
dbo.forgot_password_tokens
    SELECT 
    'random_guid' as forgot_password_tokens_id, 
    current_timestamp as updated_at,
    cd.contact_details_id,
    u.users_id
        FROM
           dbo.users as u
           inner join dbo.people as pe on pe.people_id = u.people_id           
           inner join dbo.parties as pa on pa.parties_id = pa.parties_id  
           inner join dbo.contact_details as cd on
               cd.parties_id = pa.parties_id
               and cd.value = 'devin@email.com'
               and cd.contact_details_types_id in ('guid_for_email', 'guid_for_second_email')

这是我得到的错误

ERROR:  column "updated_at" is of type timestamp with time zone but expression is of type uuid

LINE 6:   cd.contact_details_id,

我不确定为什么它说 updated_at 得到了错误的数据,我认为 current_timestamp 会把它放在正确的位置

最佳答案

目标表中的列顺序可能与您选择的列顺序不匹配。这是一个很好的例子,说明了为什么您应该始终insert 语句中指定列列表:

INSERT INTO dbo.forgot_password_tokens
   (forgot_password_tokens_id, updated_at, contact_details_id, users_id)
SELECT ...
FROM ...

关于sql - Postgres Inner join insert into 和 select from 语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37702724/

相关文章:

mysql - 内连接,具有相同 ID 的多条记录,意外的空结果集

mysql - 将两列限制为两个字段都必须有数据或两个字段都必须为 NULL

node.js - Sequelize : Unhandled rejection TypeError: admin. hasOperation_sav 不是函数

mysql - 无法创建存储函数 - 语法错误?

postgresql - db.t2.micro Postgres 实例上的 "Enable IAM DB Authentication"在哪里

c# - 应该使用哪种 C# 数据类型来存储没有时间的日期?

python - 如何根据某些条件(包括日期时间)映射多个 Pandas Dataframe 中的值?

mysql - drupal7通过手动sql获取内容

c# - 数据库查询中的存储过程与代码

mysql - 在单个查询中我想从两个不同的表获取数据?