如何将数据从一个字段逐行复制到另一个字段?
要将数据从一个字段逐行复制到另一个字段,请使用 UPDATE 命令。
我们首先创建一个表 −
mysql> create table DemoTable ( StudentId int, StudentFirstName varchar(20), StudentMarks int default 0 ); Query OK, 0 rows affected (0.49 sec)
下面是使用 insert 命令在表中插入记录的查询 −
mysql> insert into DemoTable(StudentId,StudentFirstName) values(89,'Larry'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable(StudentId,StudentFirstName) values(35,'Robert'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable(StudentId,StudentFirstName) values(48,'Chris'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable(StudentId,StudentFirstName) values(78,'David'); Query OK, 1 row affected (0.61 sec)
下面是使用 select 命令从表中显示记录的查询 −
mysql> select *from DemoTable;
这将产生以下输出 −
+-----------+------------------+--------------+ | StudentId | StudentFirstName | StudentMarks | +-----------+------------------+--------------+ | 89 | Larry | 0 | | 35 | Robert | 0 | | 48 | Chris | 0 | | 78 | David | 0 | +-----------+------------------+--------------+ 4 rows in set (0.00 sec)
下面是将数据从一个字段逐行复制到另一个字段的查询。这里,我们将将 StudentId 的所有值复制到 StudentMarks −
mysql> update DemoTable set StudentMarks=StudentId; Query OK, 4 rows affected (0.34 sec) Rows matched: 4 Changed: 4 Warnings: 0
让我们从表中显示所有记录,以检查所有行是否都已更新 −
mysql> select *from DemoTable;
这将产生以下输出 −
+-----------+------------------+--------------+ | StudentId | StudentFirstName | StudentMarks | +-----------+------------------+--------------+ | 89 | Larry | 89 | | 35 | Robert | 35 | | 48 | Chris | 48 | | 78 | David | 78 | +-----------+------------------+--------------+ 4 rows in set (0.00 sec)
广告