Home > Database > Mysql Tutorial > body text

How to Copy Specific Fields Between MySQL Tables?

Barbara Streisand
Release: 2024-11-22 18:03:17
Original
897 people have browsed it

How to Copy Specific Fields Between MySQL Tables?

Copy Data Between Tables in MySQL

Copying data between tables in MySQL is a common task for managing and maintaining databases. In this article, we will address the scenario where you need to copy specific fields from one existing table (Table 1) into a new table (Table 2) using MySQL queries.

Query to Copy Fields from Table 1 to Table 2

The following SQL query will allow you to copy data from select fields in Table 1 into Table 2:

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

This query accomplishes the following:

  • Inserts new rows into Table 2 with the specified columns: st_id, uid, changed, status, and assign_status.
  • Selects data from Table 1, including the st_id, from_uid (which is renamed to uid in Table 2), the current timestamp as changed, Pending as the status, and Assigned as the assign_status.

Note:

  • The now() function gets the current timestamp.
  • If you want to copy all rows from Table 1, the WHERE statement in the query can be omitted.
  • If you want to copy only a subset of rows, add a WHERE statement to filter the selected rows.

The above is the detailed content of How to Copy Specific Fields Between MySQL Tables?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template