I am new to SQL, I have a bunch of tables test1 test2 test3----test100. Those tables are created on a daily basis and have same columns/format. I want to create a table that should have 1 month past data, in other words i have to combine test1-test30 into one single table. I can only thinks of the below code but i am looking for something efficient and easy to do like using loops or while conditions..or some other way
create table final as
select * from test1;
insert into final from test2
insert into final from test3
.
.
insert into final from test30..
Can you suggest me a simpler way to do this instead of using 30 insert statements
Here is an example:
Copy all columns from one table to another table:
INSERT INTO table2
SELECT * FROM table1
WHERE condition;
Copy only some columns from one table into another table:
INSERT INTO table2 (column1, column2, column3, ...)
SELECT column1, column2, column3, ...
FROM table1
WHERE condition;
Another solution is to dump the data from all your database if it has the same tables then insert it into .csv or txt file and import it into your final table.
Mysql -u [username] -p [database_name] < /path/to/file.[extention]
Related
How do I copy particular columns of one table to another table like we copy whole table by going in operations menu. But I have to only move particular columns how will I
Use ALTER TABLE statement (to copy the column structures):
ALTER TABLE tbl_name ADD [COLUMN] (col_name column_definition,...)
Check out https://dev.mysql.com/doc/refman/8.0/en/alter-table.html
Then use INSERT INTO SELECT statement (to import the data):
INSERT INTO table2 (column1, column2, column3, ...)
SELECT column1, column2, column3, ...
FROM table1
WHERE condition;
Check out https://www.w3schools.com/sql/sql_insert_into_select.asp
I am managing a database that has two tables for "experience" values. I am trying to copy the values from the first experience table to the second one, but the query won't go through. This is what I've got until now.
Tables:
-mob_db_re //Target table
-mob_db //Source Table
Query syntax:
UPDATE mob_db_re
INNER JOIN mob_db ON mob_db.EXP = mob_db_re.EXP
SET mob_db_re.EXP = mob_db.EXP
If you want to copy all the rows from one table to another try using a SELECT INTO statement:
SELECT *
INTO mob_db_re
FROM mob_db;
This will work if the tables have the same schema, otherwise you will have to specify the columns.
Use this to copy one table to another
If they have same schema, use this one
INSERT INTO mob_db_re (SELECT * FROM mob_db)
If they do not have same schema, use this one
INSERT INTO mob_db_re (column1, column2, column3, ...)
SELECT column1, column2, column3, ... FROM mob_db
Also you can add WHERE condition to the above SELECT statement if needed.
For some reason, If you to need to update instead of insert, use this one
UPDATE mob_db_re
SET re.EXP= db.EXP
FROM mob_db_re re
INNER JOIN mob_db db
ON re.EXP= db.EXP;
How it's possible to copy one entry row of a table with same data to another (same ID, same data values) Database -> same table?
Example:
Table Units:
UID Department Name Item
67 HR John Doe Table
If both tables equal no. of columns and in same order you want to insert then just use below simple query-
INSERT INTO mytable SELECT * FROM units WHERE uid=67;
If you want to insert selected column in another table's selected columns and in your order then use below-
INSERT INTO mytable(col1,col2,col3,col4) SELECT uid,department,`name`,item FROM units WHERE uid=67;
If I understand you correctly you want to copy some rows to table of another DB.
Try INSERT SELECT Query:
insert into db1.tbl(id,col1,col2)
select id,col1,col2 from db2.tbl;
Use trigger option in mysql to make new table with same data.
Suppose if you want to copy table1 data to table2 with some condition.
INSERT INTO table2 (ID, NAME) SELECT Col1, Col2 FROM table1 WHERE Col1='<Your_Condition>';
Here table2 have fields like ID and NAME and table1 have fields like Col1 and Col2.
In that case, above query copy table1 data to table2 on these fields where condition matched on table1, if you want to copy whole data of table1 then remove the WHERE condition from Select Query.
I have two databases, test1 & test2. In test1 there is a table attendence, I want to copy this attendece table in database test2. I am writing following code:
CREATE TABLE test1.attendence SELECT * FROM test2.attendence;
But it gives the error:
--Table 'test2.attendence' doesn't exist
So please provide a way to do it.
insert into table2 select * from table1
or if they dont have the same structure:
insert into table2 (col, col2, col5) select (x,y,z) from table1
CREATE TABLE x LIKE other_db.y;
INSERT INTO x SELECT * FROM other_db.y;
you're almost there :)
but you want to create the new table in test2, so, the correct command should be
CREATE TABLE test2.attendence LIKE test1.attendence;
INSERT INTO test2.attendence SELECT * FROM test1.attendence;
I have a 2 database in mysql ,Now i need to import a table from one database to another but the thing is in first database the table contains 12 fields whereas in second database table it contains 8 fields in random order and the fields in both the tables are same except a few.How do i fix this ??
If both databases are on the same instance of MySQL then just use INSERT INTO ... SELECT ... FROM ... syntax and a fully qualified names for tables in format <db_name>.<table_name>. That's assuming that appropriate rights have been granted.
INSERT INTO db2.table_name (column1, column2, column3)
SELECT (column1, column2, column3)
FROM db1.table_name