Swap Column of Data in phpMyAdmin - php

I have a database table with 6 columns of 365 rows of data. I need to swap the 3rd column (named 'Date_line') with new data while leaving the other 5 columns in place, without exporting the whole table, but can't get phpMyAdmin to work with me.
Normally I'd just truncate the table and upload a revised CSV file for the whole table, but here's the catch: I have to update 232 data tables with this same exact column of data (the column data is common to all 232 tables). To do all 232 individually would mean exporting each table, opening it in Excel, swapping the old column for the new one, converting to CSV then re-uploading. It would be a lot easier if I could just import a single column CSV to overwrite the old one. But I don't know how.
I'd like to do this using the phpMyAdmin interface... I'm not much experienced in assigning scripts. Is there a way?

Related

How to import only one column from a .sql dump in MySQL

I have a database dump which consists of 7 seperate tables. In one table, one specific column was reset as i have forgotten WHERE statement because i am beginner.
What i need help about is
How can i restore only one specific column from one specific table in dump or,
Is there any way i can do this with PHP?
Please note that dump was exported 4 days ago and there are new rows on tables.
Create a new "temp" table and import the data from the dump file (just the records of the table you need to restore).
Next, execute an UPDATE + JOIN query on the broken table. The IDs are the same in both tables so it will be pretty easy.
Remove the "temp" table.

SQL CSV file - Skipping all columns after a certain column number

I've been having an issue for days now and have hit a brick wall. Firstly, as the title suggests I have been working to import CSV files to a SQL database.
To be more specific, this is done through PHP scripts on the server and through MySQL into the DB.
I currently have around 30 CSV files (this number is projected to increase) which are updated daily, then a cron script is triggered once per day to update the new data. It loads the file through LOAD DATA INFILE. All of this works perfectly.
The problem is:
Each CSV file contains a different column count. The column count ranges between 50-56 columns. The data I am storing in this collective database only requires the first 8 columns. I already know how to skip individual columns using #dummy thanks to the following Q&A: How to skip columns in CSV file when importing into MySQL table using LOAD DATA INFILE?
However, as the dummy count will not always be the same due to the different column counts, I was wondering if there was a way to get the data from columns 1-8 then ignore all after regardless of column count?
A rather rough patch up would be to first read the beginning line in php, to count columns by commas. Then knowing the amount, subtract 8 and generate the sql command now knowing how many columns you need to ignore.
Just include the eight columns to populate and it will us the first eight from the CSV row:
LOAD DATA INFILE 'file.txt' INTO TABLE t1 (c1, c2, c3, c4, c5, c6, c7, c8)

PHP / MYSQL Split XLS based on Column and Insert

I have a large excel file of ship locations for the next 2 years. Currently, I am manually splitting this file into multiple files and then importing into MYSQL. This wont work for long though, as the Excel file gets updated everyday and needs an easier way to be imported.
The data doesnt start till row 10, Column D, and goes till Column I, then another Ship starts on Row 10 Column J to O, ect, for 22 ships. (Row 10 being the header titles)
Is there a way to automate this? I have done some research and found I probably need to convert the XLS to CSV which isnt a problem, but I havnt found a way to state Row 10 Column's D-I into Table1, Column J to O to Table2, Column's P-U to Table3, ect.
Can someone point me in the right direction or provide some assistance. Thanks for all your help!
It's hard to tell not knowing all the details but I would propose the following flow:
Save your file as CVS (can be automated via VBA, PowerShell...)
Load it as it is in a staging table using LOAD DATA INFILE
If necessary validate, clean, normalize the data in the staging table
Use INSERT INTO ... SELECT ... to copy data to factual tables (selecting only necessary columns e.g. from J to O...)
Truncate the staging table
If the rules of data extraction are deterministic steps 2-5 can wrapped in a stored procedure.

Update MySQL Table using CSV file

my Current MySQL table employee_data has 13k rows with 17 columns. The data in the table came from a CSV file Employees.csv. after importing my csv data I added a new column 'password' (so its not in the csv file) Password is edited and accessed via a web portal. I now have an updated csv file and I want to update my main table with that data but I don't want to lose my password info.
Should I import my new CSV file into a temp table in my database and some how compare them? I am not sure where to start and I am open to recommendations.
I am now realizing I should have kept my password info in a separate table. Doh!
I guess I could created a php file that compares each row based on the employee_id field but with 13k rows I am afraid it would time out possibly.
I would do it like this :
Create a temp table using CREATE TABLE new_tbl LIKE orig_tbl; syntax
use LOAD DATA INFILE to import the data from the CSV into the table
Use UPDATE to update the primary table using a primary key / unique column (perhaps employee_id)
I have worked with tables containing 120 million lines and imported CSV files containing 30 million lines into it - this is the method I use all of the time - much more efficient than anything in PHP (and thats my server side language of choice)
Try other tools other than php based ones phpMyAdmin
MySQL workbench is a great tool,
based on you connection it will take a while to query the database with your data.
There are workarounds with php timeout limit,
set_time_limit();

mysql php create extra colulmn in rs using as

I have a question to which I have been unable to find the answer.
I can create an extra column in a PHP recordset by using an existing column and duplicating it:
SELECT
id_tst,
name_tst,
age_tst,
price_tst,
price_tst AS newprice_tst
FROM test_tst
From what I can work out the AS will only duplicate an existing colulmn or rename a column rs.
I want to add two extra columns to a table, but with no values.
I know a lot of people will say whats the point in that; it's pointless to have 2 columns with no data.
The reason is I am doing a price updating module for a CMS system, where the user can download a .csv file containing prices, modify the prices in a spreadsheet then re-upload the CSV to update he prices.
the two extra columns would be to hold the new prices keeping the old so a roll back from the CSV file could be performed if nessecary.
I could just get the client to add the two new colulmns into the spreadsheet, but would prefer to have the exported CSV with the columns already in place.
Is it possible to create blank columns when creaing an rs?
You can create empty "dummy" columns by aliasing a blank string:
SELECT '' AS emptyColumn, column1, column2 FROM table
This will produce another column in your query with all blank values.

Categories