简体   繁体   English

如何使用对同一数据库表的查询结果更新 MYSQL 表中的字段?

[英]How can I update a field in a MYSQL table with the result of a query over the same database table?

I have a MYSQL database with one table named students.我有一个 MYSQL 数据库,其中包含一个名为 Students 的表。 The table has three fields namely, regNumber, fullName, and email.该表具有三个字段,即 regNumber、fullName 和 email。 The email field is currently empty for all records in the table.对于表中的所有记录,电子邮件字段当前为空。 I am able to generate email addresses based on the regNumber and fullName columns.我能够根据 regNumber 和 fullName 列生成电子邮件地址。 Below is the SQL script for achieving that:下面是实现这一目标的 SQL 脚本:

SELECT  
    REPLACE(CONCAT(SUBSTRING(SUBSTRING_INDEX(SUBSTRING_INDEX(fullName, ' ', 1), ' ', -1), 1, 1),
    SUBSTRING_INDEX (SUBSTRING_INDEX(fullName, ' ', 3), ' ', -1),
    SUBSTRING(regNumber, 7, 4), "@ut.edu.ua"), '`', '') AS student_email 
FROM students;

However, I want to update the email field of every tupple with the result of executing the above script.但是,我想用执行上述脚本的结果更新每个元组的电子邮件字段。 How can I possibly achieve this?我怎么可能做到这一点?

update students
set email = REPLACE(CONCAT(SUBSTRING(SUBSTRING_INDEX(SUBSTRING_INDEX(fullName, ' ', 1), ' ', -1), 1, 1),
            SUBSTRING_INDEX (SUBSTRING_INDEX(fullName, ' ', 3), ' ', -1),
            SUBSTRING(regNumber, 7, 4), "@ut.edu.ua"), '`', '') 

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

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