简体   繁体   English

如何在SQL中将两列连接成一个新列?

[英]How to concatenate two columns into a new one in sql?

I have a problem while merging two columns into a new one. 将两列合并为新列时出现问题。 I just want to concatenate the columns IBAN and Currency_code into CheckSum. 我只想将IBAN和Currency_code列串联到CheckSum中。 And when I make a SELECT * it appears. 当我进行SELECT *它就会出现。

DROP TABLE IF EXISTS transaccio;
CREATE TABLE transaccio (
   IBAN varchar(255),
   Currency_code varchar (255),
   CheckSum varchar(255)
);

I have raised two options, but none of them works. 我提出了两个选择,但是没有一个可行。

Option one: 选项一:

INSERT INTO transaccio(CheckSum) SELECT (CONCAT(IBAN, Currency_code)) FROM transaccio;

Option two: (it says that don't know from where get the IBAN and Currency_code) 选项二:(它说不知道从哪里获得IBAN和Currency_code)

INSERT INTO transaccio(CheckSum) VALUES (CONCAT(IBAN, Currency_code));

You need to use Update query instead of Insert : 您需要使用Update查询而不是Insert

UPDATE transaccio 
SET CheckSum = CONCAT(IBAN, Currency_code);

However it seems to be a Generated Column problem. 但是,这似乎是“ 生成列”问题。 Depending on your MySQL version, you could use Generated Columns instead. 根据您的MySQL版本,您可以改用Generated Columns。

PostgreSQL does not support Generated Columns / Virtual Columns natively yet . PostgreSQL不支持生成列/虚拟列本身 You may refer this answer: https://dba.stackexchange.com/a/183265/27070 to emulate them in PostgreSQL 您可以参考以下答案: https : //dba.stackexchange.com/a/183265/27070以在PostgreSQL中模拟它们

You will need the UPDATE statement: 您将需要UPDATE语句:

UPDATE transaccio
SET CheckSum = CONCAT(IBAN, Currency_code);

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

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