Ahhhh! Need some MySQL help!

Discussion in 'MySQL' started by crazyryan, Feb 9, 2008.

  1. #1
    Hey

    I have an SQL kinda like this:
    INSERT INTO `fam_quotes` VALUES (20110, 'Ecclesiastes', 'Give not over thy soul to sorrow and afflict not thyself in thy own counsel. Gladness of heart is the life of man and the joyfulness of man is length of days.');
    INSERT INTO `fam_quotes` VALUES (20111, 'Ecclesiastes', 'Be not slow to visit the sick.');
    INSERT INTO `fam_quotes` VALUES (20112, 'Greg Evans', 'Anger at lies lasts forever. Anger at truth can\'t last.');
    INSERT INTO `fam_quotes` VALUES (20113, 'Havelock Ellis', 'The more rapidly a civilization progresses, the sooner it dies for another to rise in its place.');
    Code (markup):
    Now, what I've done is created a table called authors with the columns authorid and name, and now what I need to do is fill that table with the SQL like above, for each author I need to assign them a unique authorid (column is auto_increment so ...) and fill in their name.

    Any ideas?
     
    crazyryan, Feb 9, 2008 IP
  2. SoKickIt

    SoKickIt Active Member

    Messages:
    305
    Likes Received:
    30
    Best Answers:
    0
    Trophy Points:
    70
    #2
    INSERT INTO `authors` (`name`) VALUES ('Author 1'), ('Author 2'), ('Author 3');
    Code (markup):
     
    SoKickIt, Feb 9, 2008 IP
  3. crazyryan

    crazyryan Well-Known Member

    Messages:
    3,087
    Likes Received:
    165
    Best Answers:
    0
    Trophy Points:
    175
    #3
    Yeah, but I already have the SQL above and there is more to it, there are like, 2000 authors, is there any way to do it all at once in some godlike query?
     
    crazyryan, Feb 9, 2008 IP
  4. SoKickIt

    SoKickIt Active Member

    Messages:
    305
    Likes Received:
    30
    Best Answers:
    0
    Trophy Points:
    70
    #4
    So you want to move authors from "fam_quotes" (column #2?) to a new table?
     
    SoKickIt, Feb 9, 2008 IP
  5. crazyryan

    crazyryan Well-Known Member

    Messages:
    3,087
    Likes Received:
    165
    Best Answers:
    0
    Trophy Points:
    175
    #5
    No, I want to assign a unique ID for each author, e.g.
    author_id, author_name
    1, Einstein
    2, Shakestein

    Based on the SQL I currently have.

    I came up with:
    <?php
    set_time_limit(0);
    
    $query = mysql_query("SELECT DISTINCT(author) from `quotes`");
    while($row = mysql_fetch_array($query)) {
    	mysql_query("INSERT into authors (author_name) values ('{$row['author']}')");
    	$id = mysql_insert_id();
    	mysql_query("UPDATE `quotes` SET author_id = '" . $id . "' WHERE author = '" . $row['author'] . "'");
    }
    ?>
    PHP:
    To try and acomplish it, and it worked to an extent, however about 9K out of 21K quotes didn't get assigned an author_id for some reason.

    Anyone know why?
     
    crazyryan, Feb 9, 2008 IP
  6. SoKickIt

    SoKickIt Active Member

    Messages:
    305
    Likes Received:
    30
    Best Answers:
    0
    Trophy Points:
    70
    #6
    You can try something this:

    INSERT INTO `authors` (`author_name`) SELECT DISTINCT(`author`) FROM `quotes`;
    UPDATE `quotes` SET `author_id` = (SELECT `author_id` FROM `authors` WHERE `quotes`.`author` = `authors`.`author_name`);
    Code (markup):
     
    SoKickIt, Feb 9, 2008 IP
    crazyryan likes this.
  7. crazyryan

    crazyryan Well-Known Member

    Messages:
    3,087
    Likes Received:
    165
    Best Answers:
    0
    Trophy Points:
    175
    #7
    Nevermind, mysql errors were actually there but not being shown, I used mysql_real_escape_string on the inserts and crap and it worked fine.

    Thanks for the help though!
     
    crazyryan, Feb 10, 2008 IP