Jump to content

Recommended Posts

I'm importing an Access file into MySQL and there's a lot of redundant data. I have only 100 authors over 80,000 rows. I know I should make a seperate authors table and replace the authors with numbers. Is there any quick way to do this?

 

How I'm doing it is first in MySQL.

SELECT author FROM books GROUP BY author;

 

Then copying the results into Notepad.

author1

author2

...

author100

 

Then turning every line into SQL.

 

UPDATE books SET author = 1 WHERE author = 'name1';

UPDATE books SET author = 2 WHERE author = 'name2';

...

UPDATE books SET author = 100 WHERE author = 'name100';

 

I can write a macro to do it fairly quick but there must be a better way?

Link to comment
https://forums.phpfreaks.com/topic/68735-solved-automatic-normalization/
Share on other sites

<?php
$sql = "CREATE TABLE author (
    authorID int not null auto_increment primary key,
    name varchar(50)
    )";
mysql_query($sql);

$sql = "INSERT INTO author (name)
        SELECT DISTINCT author FROM books";
mysql_query($sql);

/**
* add a new column "authorID" to books table with phpmyadmin or similar, then
*/

$sql = "UPDATE books b INNER JOIN author a ON b.author = a.name SET b.authorID = a.authorID";
mysql_query($sql);

/**
* now drop column "author" from books table
*/

?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.