Phpmyadmin - import new column to existing records
I already have a table in phpmyadmin that contains users records. Each user has a unique admission number. I now want to add a new column to this table and was wondering how I can import data for this new column using just the admission number and new data.
Is this possible? I have a CSV but can't work out the best way to import the data without overwriting any existing reco开发者_运维百科rds.
Thanks.
As far as I can see, this is not possible. phpMyAdmin's import features are for whole rows only.
You could write a small PHP script that opens the CSV file using fgetcsv()
, walks through every line and creates a UPDATE statement for each record:
UPDATE tablename SET new_column = "new_value" WHERE admission_number = "number"
you can then either output and copy+paste the commands, or execute them directly in the script.
If you want to do it using just CSV, here are the steps you could perform.
- In a text editor, make a comma separated list of all the column names for the final table (including your new column). This will be useful for importing the new data.
- Add the new column to your table using phpmyadmin
- Export current table in csv format and sort by admission number in Excel
- In your new data CSV, sort by admission number
- Copy the column over from your new data to your exported CSV and save for re-import.
- Backup your users table (export to CSV)
- Truncate the contents of your table (Operations, Truncate)
- Import your updated CSV
Optional / Recommended: When you import CSV into phpmyadmin, use the column names option to specify the columns you are using, separated by commas (no spaces).
Assumptions: 1. You are using Spreadsheet such as Excel / OpenOffice to open your csv files.
Any problems? Truncate the table again and import the sql backup file.
精彩评论