简体   繁体   English

如何在插入语句中连接来自另一个表的两列值?

[英]How to concatenate two columns values from another table in the insert statement?

I have a table Comment and I need to insert the values to the table Comment from another table ProComment as shown here: 我有一个表Comment ,我需要将值插入另一个表ProComment的表Comment ,如下所示:

INSERT INTO Comment (id, insertdate, commenttext, reviewdate) 
    VALUES (1, GETDATE(), 'This is the new review period from ', GETDATE())

But I want to get the reviewstartdate and reviewenddate from ProComment and need to append with commenttext in the Comment table, like this: 但是我想从ProComment获取reviewstartdatereviewenddate ,并需要在Comment表中添加commenttext ,如下所示:

INSERT INTO Comment (id, insertdate, commenttext, reviewdate) 
    VALUES (1, GETDATE(),
        'This is the new review period from ' + (SELECT reviewstartdate, 'to ', reviewenddate FROM ProComment WHERE id = 1), 
        GETDATE())

Expected results will be "This is the new review period from 2018-05-05 to 2019-05-05" on the comment section 预期结果将在评论部分为“这是从2018-05-05到2019-05-05的新审查期”

You need to get rid of the Table Value Constructor values and put a regular select statement including CONCAT() function or + operator to concatenate the strings : 您需要摆脱表值构造函数的 values并放置一个包含CONCAT()函数或+运算符的常规select语句来连接字符串:

insert into Comment( id, insertdate, commenttext, reviewdate) 
select 1, getdate(),
       concat('This is the new review period from ', reviewstartdate,' to ',reviewenddate), 
       getdate()
  from ProComment 
 where id = 1

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 insert语句来自另一个表的一列其余列是值 - insert statement one column from another table rest of the columns is values 如何使用Insert语句将两列或更多列数据插入一列,从一张表到另一张表 - How to insert two or more columns data into one column form one table to another table using Insert statement 如何根据另一个表中多个列的值从一个表中提取多行,然后在 SQL 中连接? - How to extract multiple rows from a table based on values from multiple columns from another table and then concatenate in SQL? 如何在select语句中连接可能包含NULL值的两列? - How to concatenate two columns that might contain NULL values in a select statement? 将两列转换为另一个表中的值? - Convert two columns to values from another table? 仅插入另一个表中的两列数据,并将 rest 保留为默认值 - Insert only two columns data from another table and leave the rest with default values 将两个不同表中的两列插入SQL中的另一个表中 - Insert two columns from two different tables into another table in SQL 如何通过设置另一个表中的值来创建要插入到表中的MySQL语句 - How to create MySQL statement to insert into a table by setting values from another table 连接两列值 - Concatenate two columns values 当一个(或多个)列的值需要从另一个表派生时,如何将多行插入到 SQL 表中? - How to insert multiple rows into an SQL table when the values for one (or more) of the columns need to be derived from another table?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM