Home >Database >Mysql Tutorial >How to Copy Data from One MySQL Table to Another Using Queries?

How to Copy Data from One MySQL Table to Another Using Queries?

DDD
DDDOriginal
2024-11-23 12:48:13225browse

How to Copy Data from One MySQL Table to Another Using Queries?

Copying Data from One Table to Another in MySQL Using Queries

Copying data between tables in MySQL is a common task for data management and manipulation. Let's delve into how this can be achieved.

In this scenario, you have two tables: Table 1 (an existing table) and Table 2 (a new table). Your goal is to copy data from specific fields of Table 1 into Table 2.

To accomplish this using MySQL queries, you can utilize the following INSERT statement:

INSERT INTO table2 (st_id, uid, changed, status, assign_status)
SELECT st_id, from_uid, now(), 'Pending', 'Assigned'
FROM table1

This query will populate Table 2 with the following data from Table 1:

  • st_id
  • from_uid (as uid)
  • Current timestamp (as changed)
  • 'Pending' as status
  • 'Assigned' as assign_status

By default, this query will copy all rows from Table 1. However, you can add a WHERE clause to specify a condition if you want to copy only a subset of rows.

The above is the detailed content of How to Copy Data from One MySQL Table to Another Using Queries?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn