Is it possible to merge data from csv to mysql? - mysql

I've csv file that contains email + street address. I've wordpress installation with users that have same email and I'd like to import street addresses to respective users in user_meta table.
How can I do this?
One stupid way that comes to mind is to use text editor search and replace to create a huge list of insert queries.
Any better way to do this?

You can use
LOAD DATA INFILE your_file.csv
INTO your_table
References: Load data - MySQL

Related

MySQL: Export multiple rows containing specific id from multiple tables all at once

I got 2 databases, an old one and a new one, with the exact same structure.
I got a main users table and all the tables in the database got an auto-incremented id columns as primary key, and a email column which is used to link each row in each table to a user.
The specific id I want to extract is the email.
They are no constraints and no foreign key.
The relationship is 1-n and 1-1 from the user table to the others ones.
(and yes, I am fully aware it is a really poorly designed database but it isn't mine)
I want to extract multiple users from the old database to the new one, with the corresponding data in all others tables.
AFAIK, you can do it without any problems from each tables, but I want to do it "database wide", like all tables at once.
All the other posts I've run into are to export multiples tables with the whole data in it, but I just want specific users.
I know there is a global search function in PhpMyAdmin where i can simply enter the email and it sort me all the matches in the databases like here
My problem would have been solved if there were an export option on this page but there isn't, and I'm not gonna browse each table for each user to extract the data manually.
With all these years working with MySQL, I was quite surprised that they isn't a native solution to this problem.
So I've decided to write a procedure with a array containing all the emails.
BEGIN
SET #array = 'test#test.com,hello#test.com';
SELECT * from users WHERE FIND_IN_SET(email, #array);
SELECT * from address WHERE FIND_IN_SET(email, #array);
SELECT * from messages WHERE FIND_IN_SET(email, #array);
SELECT * from tickets WHERE FIND_IN_SET(email, #array);
END
But then, I've run into another problem, I can't export a procedure result (I've opened another topic for that), and even if I could with a workaround I've tested, I can only do it table by table since I can't export multiple SELECT at once.
I'm currently working on it, but I would rather ask if there is a move convenient solution since i'm pretty sure i'm not the only one who has run into this problem.
Thanks in advance,
EDIT
Thanks to #Akina, managed to export it with the following query to get a proper CSV:
SET #array = 'test#test.com,hello#test.com';
SELECT * INTO OUTFILE 'out.csv'
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '
'
FROM users WHERE FIND_IN_SET(email, #array);

when we use json data type in database?

I want to ask..
1. When do we use JSON data type in a database?
2. What about performance if the data size is big?
Example:
I have user credential table with the following columns:
name
email
password
status
phone
and I want to add adittional data like address, sex, photo, zip code
What is the best solution?
Just add the columns into the existing table user
Create a new table
Create a new json data type
I am sorry if the question is basic..
Thanks in advance
To use json data type in table assume that data is stored in json format by using bytea/blob we can save json in postgresql database.
Its better to Alter table and Add columns to existing table.
Performance of Database directly related to Memory or Space available in Computer/Server (so it doesn't matter if data is big)

Adding unknown number of values to MySQL database

My situation is that I am creating a wordpress plugin, it creates a table in the directory on activation.
This table holds information entered into the plugin, when information is being entered the user has the option to upload images. The number they will choose to upload I do not know.
The issue I am having is figuring out how to add these URLS into the database, sure I can just put them in there but again I do not know how many URLS need to be added.
As I cannot use normalization with wordpress, how would I store the URLS in the DB. Say they upload 5 images but I do not have 5 separate columns (URL 1, URL 2...)
I should also note that these images will be fetched using a for loop, so each image will be sent off regardless of the number they are uploading.
Any help would be appreciated
Cheers.
Also, You can make additional table with:
id, information_id, filename, ordr
Firstly, insert into infromation_table, get last inserted ID
Then, insert into filenames_table all your filenames in order
Finally, you can select data:
SELECT i.id, i.title, GROUP_CONCAT(a.filename ORDER BY a.ordr ASC SEPARATOR ',') AS filenames
FROM information_table AS i
JOIN filenames_table AS a ON (a.information_id = i.id)
If you want to store all image urls or other data into one DB row, you must create an array with all the information that you need, and then using serialize you can put this array in one filed in the database.
This filed must be TEXT or LONGTEXT to be able to collect all this information.
Then, when you get data back, you must use unserialize and data will be converted to arrays again if needed.
This is how WP stores information in the postmeta table.
Note that in this way you cannot easily query the data, so if this is important to you maybe will be better to store each image on separate row.

imdb_id is NULL for all names and titles

I have downloaded sample dump of imdb (mySQL) from imdbpy as mentioned in the following URL:
http://blog.secaserver.com/2013/08/importing-imdb-sample-data-set-mysql/
After installing all the data, i observed that imdb_id is NULL in title and names tables. I have few queries in this:
How to fill the imdb_id in those tables?
My main intention is to get the poster URLs for movies and headshots for the actors. Is there any simple way to import those URLs in mysql database?
What is the use of md5sum in the title and names tables?
The imdbID information is not distributed with the plain text data files, so the NULL values are expected. The field is automatically filled when a Movie object is asked for it's imdbID, doing a remote query (with a title match).
I'm not aware of any easy and fast way to populate it, sorry.
So, to aswer your questions:
just do a (remote) query as described here: http://sourceforge.net/p/imdbpy/mailman/message/1053124/ (it goes without saying that you can populate the complete table).
headshots and posters URLs are no available in the plain text data files; you have to use IMDbPY to access the data from the website.
a checksum of the 'long imdb canonical name' or 'long imdb name', mostly used to preserve imdb_id values during the upgrade of an existing database (importing a new set of plain text data files).

Need to Change ids of records from SugarCRM Export for Accounts and Contacts

I have a bit of a complex issue to deal with here at the moment and would love some help please.
I have exported data from SugarCRM in CSV format to be imported in to my new app. Now my app works slightly differently in terms of database schema. I use auto-incrementing integers for id's where as SugarCRM uses long alphanumeric strings like dc4c072c-ec0e-26b6-8780-4a9e7ec8375d.
I need to be able to take the data for say accounts and contacts, using the SugarCRM pivot table export from accounts_contacts, and change the id's, while keeping them all linked correctly. I then need to use the changed data to import in to my database, where each record in the contacts table contains an account_id field which links them to accounts, rather than a pivot table.
Now, my first thoughts would be to import all the required data in to clean database and use a lookup table with four fields in it to change the id's. So I would have old_account_id, new_account_id, old_contact_id, new_contact_id. I'd then use that table to find the right id's and amend the data.
The only issue I have with this, is that my SQL isn't amazing, so I'm having difficulty visualising and writing the query to use the lookup table and change the data. So I would love some help with this.
Also, once I have the query and the correct amended data, I need another query to then create the contacts table using an account_id field from the pivot table.
Hope you guys can help out. Thanks in advance.
Are you planning on keeping the data in sync? I'd suggest adding a "sugar_id" column in your app and a "your_app_id" column in SugarCRM. This way the native id column remains the same and that it will maintain a reference to the record in the other system.