Importing mysql data from one table to another - mysql

It's very simple to someone but looks like complicated to me. I have 2 tables, one is called jos_users other is called jvdb_users. Tables are exact same, both containing user informations such as user email, password, name, etc. Only table one have many users, table two is empty, i need to transfer all fields and data from table one to table two. How do i do that with phpmyadmin and mysql on linux server.

if they both have the same columns you can use this command
INSERT INTO `tabletwo` (SELECT * FROM tableone)
You can write this in the SQL section of phpmyadmin

try this :
INSERT INTO table1 VALUES (SELECT * FROM table2);

Related

Is it possible in MySql to associate inserted data to a specific user?

I just was wondering if is it possible to associate an user to the data inserted in a table.
For example:
I have a table Customers with columns customerID, name and address. Is there any method in MySql to know what user has inserted each row in the table or must I add a column userID in the table Customer to add the user in my SQL Insert statement?.
If I have only one table, it is not a proble to add the userID column. But when i have multiple tables, maybe it becomes a messy task.
Thanx in advance.
If you are trying to find out the user_id caused the insert then NO, there is no other way than you storing it explicitly likewise you already have thought of.
If there is multiple tables for which you want to store the same information; then you can probably have a separate table where you can have the user_id column and can use a AFTER INSERT TRIGGER to insert the user id in this table.
No, no such functionality is provided by MySQL. You'll have to add a column for user_id in your table(s) and insert the user id yourself.

Importing sql file data omitting columns (MySql)

I am building a new booking system in PHP at the moment and I want to take over most of the data of the old system (MySQL) however the database structure of the new system will be slightly different. However I think I could take over some of the tables. Is it possible to import rows of old tables to the new table given that the column names are equal but some column names in the new table might be missing?
an example, the old table:
tourguides
id name address email telephone_mobile telephone_home
the new table
tourguides
id name address telephone_mobile
the new table doesn't has the column telephone_home, this shouldnt stop the import but instead it should be ignored
Yes, it's possible.
You should do an INSERT SELECT with fixed fields, like this:
INSERT INTO table1 (fieldX, fieldY) SELECT fieldX, fieldY FROM table2
You can even do it between databases:
INSERT INTO db1.table1 (fieldX, fieldY) SELECT fieldX, fieldY FROM db2.table2

Inserting into a table from an incompatible table

I have a MySql table called Person, and one day I accidentally deleted someone from this table. I have a backup table, called PersonBak so I was going to restore my deletion from the backup. However, in the course of moving forward on my application I renamed all the fields in Person, except for the primary key, PersonID. Now Person and PersonBak have the same data, but only one matching column name.
Is there any way to restore my missing person to Person from PersonBak without doing a lot of work? I have quite a few columns. Of course I could just do the work now, but I can imagine this coming up again.
Is there some way to tell MySql that these are really the same table, with the columns in the same order, just different column names? Or any way at all to do this without writing out specifics of which columns in PersonBak match which ones in Person?
If the column datatypes are the same between the tables, the column count is the same, and they are all in the same order, then MySQL will do all of the work for you:
INSERT INTO t1 SELECT * FROM t2;
The column names are ignored. The server uses ordinal position only, to decide how to line up the from/to columns.
What about this:
insert into Person(id, col11, col12) (select id, col21, col22 from personBak where id=5)
person schema:
columns (id, col11, col12)
personBak schema:
columns (id, col21, col22)
Look at Mysql SELECT INTO and you can specify the field names & create an insert statement

inserting data from one mysql table into another mysql table using a form

I'm still very new in mysql scripting and I need help on the above matter.
I have two table consist of same field names except one field as below:
table 1
id_student, studentname, studentnric, studentno, dateofbirth, address, phone, courses, session
table 2
id_graduate, studentname, studentnric, studentno, courses, session
What I am trying to accomplish is, to get the data in the first table into the second table
INSERT INTO table2
SELECT id_student as id_graduate, studentname, studentnric, studentno, courses, session
FROM table1
Take a look at insert into select
http://dev.mysql.com/doc/refman/5.0/en/insert-select.html

SQL: flavours of INSERT INTO

(MySQL) I am trying to migrate a "subscription" table into 3 new tables: "product", "subscription", "actual" where "actual" is the name of the actual product, say, newsletter. The subscription has a FK reference to product and the actual has FK reference into subscription.
I know the INSERT INTO SELECT statement can copy data probably from many table to one; is there a statement to do the opposite, one to many tables for my case?
I'm not aware of an SQL statement that will do what you want. Just do several INSERT INTO SELECTs one after the other. It may be faster to do them one at a time anyway.
I think you can use three seperate insert into select statements. First you convert the product table, then the subscription where you can use an embedded select to find the id in the product table:
insert into subscription (some_column, FK_id,...)
select something, (select id from product where <your where clause>),...
and finally convert the actual table using an embedded select to get the id from the subscription table.