How about
怎么样
UPDATE TABLE1 T1 SET
real_name=((SELECT CONCAT(firstname, ' ', lastname) FROM TABLE2 WHERE username = T1.audit_username ))
It's unclear to me whether you just want to execute a query or to really change the data in table1.
If you want the query only:
我不清楚您是只想执行查询,还是想真正更改表1中的数据。如果您只想要查询:
SELECT
t1.audit_username,
CONCAT(t2.first_name, ' ', t2.second_name) AS real_name
FROM
table1 t1 INNER JOIN table2 t2
ON t1.audit_username = t2.username;
If you want to change the data:
如果您想要更改数据:
UPDATE
table1 t1 INNER JOIN table2 t2
ON (t1.audit_username = t2.username)
SET
t1.real_name = CONCAT(t2.first_name, ' ', t2.second_name);
See here this will work correctly.
请看这里,这将正常工作。
Note: Take care to use the correct syntax to update the table! The syntax provided in this answer is invalid for a MySQL
DB.
注意:请注意使用正确的语法更新表!此答案中提供的语法对于MySQL数据库无效。
I recommend to read some documentations, for example this one about the correct syntax for such update commands.
我建议阅读一些文档,例如这篇关于此类更新命令的正确语法的文档。
You can use an UPDATE statement with a JOIN to achieve this. Here's an example of how you can do it:
您可以使用带有联接的UPDATE语句来实现这一点。下面是一个如何做到这一点的例子:
UPDATE table1
SET real_name = CONCAT(table2.first_name, ' ', table2.second_name)
FROM table1
INNER JOIN table2 ON table1.audit_username = table2.username;
This SQL query will join table1
and table2
on the username
field, and then update the real_name
field in table1
with the first_name
and second_name
fields from table2
, separated by a space.
该SQL查询将在用户名字段上联接Table1和Table2,然后使用Table2中的First_Name和Second_Name字段更新Table1中的REAL_NAME字段,用空格分隔。
To insert values from two columns in one table into a single column in another table using SQL, you can use the INSERT INTO statement along with a SELECT query to retrieve the values from the source table. Here's a general syntax for such an operation:
要使用SQL将一个表中两列中的值插入另一个表中的单个列中,可以使用INSERT INTO语句和SELECT查询从源表中检索值。下面是此类操作的一般语法:
Example-
示例:
INSERT INTO destination_table (destination_column)
SELECT source_column1 || ' ' || source_column2
FROM source_table;
INSERT INTO Destination_TABLE(Destination_Column)SELECT SOURCE_COLUNN1||‘’||SOURCE_COLUNN2 FROM SOURCE_TABLE;
更多回答
INSERT is not applicable here as the questioner wishes to update existing rows in Table1
此处不适用INSERT,因为提问者希望更新表1中的现有行
我是一名优秀的程序员,十分优秀!