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
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 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]
We have database table 'field_data_body' and we are using 'insert into ... select from' mysql command to insert data into 'field_data_body' from another table.
Table structure:
In our database table, delta column is used to differentiate same record. For example,
In above exmaple, both row has same data except different value of delta.
How can we set delta while inserting data into database table?
I have search in google and some other questions in stack exchange but did not find solutions.
Thanks in advance.
You could use 2 queries for that.
insert into tablename (col1, col2, ..) Values (value1, value2, ..);
insert into tablename (delta) Values (value) where entity_type = 'node';
I have the following csv....
6353,test
7855,test2
I only want the second field to be imported. Ive tried the following for the specified column names in phpmyadmin
null, product_name
But no luck
You can simply write the columns you don't need into variables.
LOAD DATA INFILE 'file.csv'
INTO TABLE t1
(column1, #var1)
Let's say that I do a MySQL insert statement, of say:
"INSERT into tablename (column1, column2, column3) VALUES ("blah", "blahblah", "blahblahblah");
Is there anyway to know what row was created? Like, let's say there's a column4 which I am using as a primary, autoincrementing key, and I want to know which is the recently submitted value. Is that possible without querying the database again?
Try using mysql_insert_id() found here:
http://php.net/manual/en/function.mysql-insert-id.php