How to import selected fields in mysql - mysql

For example I have a table. With a field Idnumber. And the field has 10 data(ex: 1234, 1235, 1236, etc)
And I have another table, subjects. Is it possible to import the data that is in the table student(field idnumber) and put it on the table subjects(field idnumber)?
So that I won't have to copy and paste the data that is in the source table to the destination table.
I'm talking about phpmyadmin

Perhaps something like this?
INSERT INTO `subjects`(`idnumber`) SELECT `idnumber` FROM `studends`;
The INSERT command can use data provided by a simple SELECT, so you can "copy" data from one table to another using this kind of syntax.

Related

How can I import a table by adjusting the record IDs to the existing table?

By default, in phphmyadmin, when exporting a table, there is a string "DROP TABLE IF EXISTS table name;", which means that when importing an exported table, it will replace an existing one (i.e., it will erase the records and insert new ones).
I need to import additional data into an existing table.
But, as I said, the imported table completely replaces the existing one in the database.
And I need THE NEW TABLE TO COMPLEMENT THE "OLD" ONE.
I believe that if the "old" table has the last id=10, then the next id of the imported table will be -11.
(I import the mysqlfile)
Please tell me how to implement this?
I decided to do this:
rename a table that already exists in the database
importing a new table
combine with UNION
You know the easiest way-write

Csv to Mysql INSERT INTO the last row

I have a problem, when I import a data from .csv file to Mysql, this data is inserted in a new row. I want this data next to the other cells, in the same row.
I have an auto-incremental Id,and the default/expression of the columns is NULL. I don't know where is the problem.
Can you help me?
Thanks!!
Assuming that the imported data rows have some columns that have values in common with the existing data (auto-incrementing IDs won't do it), import the data into a temporary table, then use an UPDATE statement to join your base table to the temporary table on the common columns, and transfer the data from the temporary table to the base table.

Cant copy unique records from one database table to another?

Hi,
I am trying to copy unique records from a database table to another table of the same name but different database. The source database contains some records that are already present in the destination database, so those I dont need, only the other ones. Database destination is called "test" and the source database is "forums". The table name is store for both cases. I am using this query:
INSERT INTO test.store (cs_key, cs_value, cs_array, cs_updated,cs_rebuild)
SELECT DISTINCT cs_key, cs_value, cs_array, cs_updated,cs_rebuild
FROM forums.store
But I am getting many errors as I try to run this query. Why?
Thank you.

MySQL comparing existing records

I just want to get suggestions about what is the best way to do the following:
I have a csv file, this file contains users and their information. Before I enter that to the MySQL database, I need to compare the email column of the CSV file with the email column in the database, it if exist, I don't insert it, if it doesn't then I insert it. How would I do this process other than manually? I would highly appreciate ideas.
Just to mention, the way I am doing it is so manually (I know it is stupid but I am not that good with SQL) what I do is, I sign in to my wordpress, go to users, and in the search bar I search for every email to make sure it does not exist.
If there's a unique index on the email column, you can use INSERT IGNORE. This will skip any records that have duplicate keys.
Insert the csv to a tempoary table. then insert into the target table by select all the records that is not exist in the target table from source table by using insert into select.

MySQL data import auto-increment issue

I am attempting to import data into a table that has a field as follows:
result_id
This field is set to AUTO_INCREMENT, PRIMARY and UNIQUE.
The data I am importing has information in the result_id field that is the same (in places) as the current data in the table. SQL won't let me import as there are duplicates (which is fair enough).
Is there a way to get SQL to append the data I am importing and not use the duplicate data in result_id, basically to continue the number within the SQL field. The reason I am asking is that I am importing about 25,000 records and I don't want to manually have to remove or alter the result_id information from the data being imported.
Thanks,
H.
How are you importing your data to MySQL?
If you are using SQL queries/script, then there should be something like INSERT INTO.... Open the file in some text editor and replace all INSERT by INSERT IGNORE. This will ignore inserting rows with duplicate primary keys.
Or alternatively if you want to replace older data with same primary keys to that in your import script, then simply use REPLACE query in place of INSERT query.
Hope it helps...
[EDIT]
Since you have Primary key, auto increment. In your table in which you want to import data, add a dummy column say "dummy" and allow it to be NULL. Now, in your import script there will be statement like INSERT INTO () values (). Now in the list of column names replace "result_id" by "dummy" and execute the script.
After executing script simply remove "dummy" column from table. Though it is bit dirty and time consuming but will do your work.