Copy data into another table
If both tables are truly the same schema:
INSERT INTO newTable
SELECT * FROM oldTable
Otherwise, you'll have to specify the column names (the column list for newTable
is optional if you are specifying a value for all columns and selecting columns in the same order as newTable
's schema):
INSERT INTO newTable (col1, col2, col3)
SELECT column1, column2, column3
FROM oldTable
How do I copy data from one table to another in postgres using copy command
You cannot easily do that, but there's also no need to do so.
CREATE TABLE mycopy AS
SELECT * FROM mytable;
or
CREATE TABLE mycopy (LIKE mytable INCLUDING ALL);
INSERT INTO mycopy
SELECT * FROM mytable;
If you need to select only some columns or reorder them, you can do this:
INSERT INTO mycopy(colA, colB)
SELECT col1, col2 FROM mytable;
You can also do a selective pg_dump and restore of just the target table.
Copy data from one table to another table in MySQL in batches based on datetime
Look at this code:
CREATE PROCEDURE `insert_data` ( IN date_from DATE,
IN date_till DATE )
BEGIN
-- adjust input dates according to the values present in the table
SELECT GREATEST(DATE(MIN(check_time)), date_from),
LEAST(DATE(MAX(check_time)), date_till)
INTO date_from, date_till
FROM test_data;
-- copy the data day-by-day
WHILE date_from <= date_till DO
INSERT IGNORE INTO test_arc
SELECT *
FROM test_data
WHERE check_time >= date_from AND check_time < date_from + INTERVAL 1 DAY;
SET date_from = date_from + INTERVAL 1 DAY;
END WHILE;
END
fiddle with debug output.
You may add a transaction into the cycle body if needed.
Copy data from one table to another table that has the same id
Use JOIN
in your UPDATE
:
UPDATE p
SET p.productImageURL = i.ImageURL
FROM Product p
INNER JOIN ProductImage i
ON i.ProductID = p.ProductID
SQL Server copy all rows from one table into another i.e duplicate table
Duplicate your table into a table to be archived:
SELECT * INTO ArchiveTable FROM MyTable
Delete all entries in your table:
DELETE * FROM MyTable
mysql copy complete row data from one table to another with different fields
You should use INSERT ... SELECT
instead of INSERT ... VALUES
and pass NULL
for history_id
:
INSERT INTO history_table
SELECT null, m.*
FROM master_table m
WHERE m.id = 8;
See the demo.
Related Topics
Select Count(*) from Multiple Tables
How to Force Postgres to Use a Particular Index
How to Select from Subquery Using Laravel Query Builder
Transpose Latest Rows Per User to Columns
Combining Union and Limit Operations in MySQL Query
Get Join Table as Array of Results With Postgresql/Nodejs
Ora-00918: Column Ambiguously Defined in Select *
How to Import a Json File into Postgresql
Calculating Cumulative Sum in Postgresql
How to Pivot in Sqlite or I.E. Select in Wide Format a Table Stored in Long Format
What Are Valid Table Names in Sqlite
Where Is |Datadirectory| Defined
How to Simulate Unpivot in Access
Select Statement to Find Duplicates on Certain Fields
SQL Query to Return Differences Between Two Tables