在 mysql 数据库中复制行-ag捕鱼王app官网

在 mysql 数据库中复制行

作者:迹忆客 最近更新:2024/03/25 浏览次数:

今天的主题是关于在 mysql 数据库中复制行。我们将学习如何复制一行并将其粘贴到具有自动增量 id 和自定义 id 的同一个表中。

我们还将了解如何从一个表的多行复制多个字段并将它们粘贴到另一个表中。


在 mysql 数据库中复制行

当需要另一个表中的两个或多个精确列时,复制行很有用。我们可以将一行(一条记录)从一个表复制到另一个表,而不是手动插入它们。

我们可以使用不同的方法来复制 mysql 数据库中的行。首先,为了学习这些方法,让我们创建一个包含两个表 tb_studentstb_attendancetest 数据库。

示例代码:

#createadatabasecreateschema`test`;#createstudentstablecreatetable`test`.`tb_students`(`id`intnotnullauto_increment,`firstname`varchar(45)notnull,`lastname`varchar(45)notnull,`department`varchar(45)notnull,`gender`varchar(10)notnull,`phone`bigintnotnull,`city`varchar(45)notnull,primarykey(`id`));#createattendancetablecreatetable`test`.`tb_attendance`(`id`intnotnull,`firstname`varchar(45)notnull,`lastname`varchar(45)notnull,`gender`varchar(45)notnull,`dept`varchar(45)notnull,`attendance`datetimenotnulldefaultcurrent_timestamp,primarykey(`id`));

在 mysql 数据库中使用 insertselect 语句将行从一个表复制到另一个表

我们必须将数据插入到一个表中,才能将该表中的行复制到另一个表中。为此,我们按如下方式填充名为 tb_students 的表。

示例代码:

#insertdataintothetableinsertinto`test`.`tb_students`(firstname,lastname,department,gender,phone,city)value('mehvish','ashiq','computer science','female','1234567890','lahore'),('thomas','christopher','physics','male','2546317908','miami'),('daniel','james','business administration','male','7854123690','texas'),('saira','kethy','history','female','3254169870','michigan');#displaytabledataselect*fromtest.tb_students;

输出:

现在,从 tb_students 表中复制数据并将其插入 tb_attendace 表中。请注意,我们不是复制所有数据,而是复制特定字段以填充 tb_attendance 表。

#insertdataintothetableinsertinto`test`.`tb_attendance`(id,firstname,lastname,gender,dept)selectid,firstname,lastname,gender,departmentfrom`test`.`tb_students`;#displaytabledataselect*fromtest.tb_attendance;

输出:


使用 insertselect 语句在具有自动增量 id 的同一表中复制行

在本节中,我们从 tb_students 表中复制一行,其中包含四条记录,使用自动增量 id 插入它,并更新 phonecity 属性的值。

示例代码:

insertinto`test`.`tb_students`(firstname,lastname,department,gender,phone,city)selectfirstname,lastname,department,gender,'2564138790','dubai'from`test`.`tb_students`where`test`.`tb_students`.id=1;select*from`test`.`tb_students`;

输出:


使用 insertselect 语句复制具有自定义 id 的同一表中的行

如果我们不想在 tb_students 表中使用自增 id 而是自定义 id,我们可以在 insert 语句中写入列名,在 select 语句中写入其值,如下所示。

示例代码:

insertinto`test`.`tb_students`(id,firstname,lastname,department,gender,phone,city)select10,firstname,lastname,department,gender,'2564138790','dubai'from`test`.`tb_students`where`test`.`tb_students`.id=1;select*from`test`.`tb_students`;

输出:

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

如何在 mysql 中声明和使用变量

发布时间:2024/03/26 浏览次数:115 分类:mysql

当你需要在 mysql 中的脚本中存储单个值时,最好的方法是使用变量。变量有不同的种类,有必要知道何时以及如何使用每种类型。

发布时间:2024/03/26 浏览次数:176 分类:mysql

本教程演示了如何在 mysql 中重置自动增量。

在 mysql 中实现刷新权限

发布时间:2024/03/26 浏览次数:211 分类:mysql

本教程介绍了 mysql 中的刷新权限命令,用于刷新授权表并影响允许的更改。

在 mysql 中设置时区

发布时间:2024/03/26 浏览次数:93 分类:mysql

在本教程中,我们将学习如何在 mysql 服务器中更改时区。

发布时间:2024/03/26 浏览次数:199 分类:mysql

本教程演示如何在 mysql 中转换为整数。

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便
网站地图