开发者

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.

  1. 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.
  2. Add the new column to your table using phpmyadmin
  3. Export current table in csv format and sort by admission number in Excel
  4. In your new data CSV, sort by admission number
  5. Copy the column over from your new data to your exported CSV and save for re-import.
  6. Backup your users table (export to CSV)
  7. Truncate the contents of your table (Operations, Truncate)
  8. 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜