#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2012
    Posts
    150
    Rep Power
    3

    Question Insert into Select giving error


    Hi all,

    The relevant set up:

    Table_1
    Colum_A
    Column_B <-- New

    Table_2
    Column_A
    Column_B

    What I'm trying to do, is add the Column B data (from Table_2) into Table 1 where Column's B from both tables match.

    Code:
    INSERT into Table_1 (Column_B) SELECT Table_2.Column_B FROM Table_2 WHERE Table_1.Column_A = Table_2.Column_A
    The error I get (while using phpMyAdmin) is:
    #1054 - Unknown column 'Table_1.Column_A' in 'where clause'
    Column_A deffinately exists in Table_1 and has data.

    Any ideas why this error is occuring?
  2. #2
  3. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,965
    Rep Power
    9397
    Sounds like you want to update existing data, not create new rows. Use an UPDATE query: make it look like a SELECT except it sets values.
    Code:
    UPDATE Table_1 JOIN Table_2 ON Table_1.Column_A = Table_2.Column_A SET Table_1.Column_B = Table_2.Column_B
    As for the error, the INSERT and the SELECT parts are separate from each other. MySQL will do the SELECT by itself, gather up the columns, then match them up with the columns in the INSERT portion and start adding data. The two parts don't know about each other.
  4. #3
  5. SQL Consultant
    Devshed Supreme Being (6500+ posts)

    Join Date
    Feb 2003
    Location
    Toronto Canada
    Posts
    27,120
    Rep Power
    4274
    Originally Posted by we5inelgr
    What I'm trying to do, is add the Column B data (from Table_2) into Table 1 where Column's B from both tables match.
    you mean where the column A's match

    and yes, this requires an UPDATE, not an INSERT/SELECT
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2012
    Posts
    150
    Rep Power
    3
    Yeah, correct

    Got it working. Thanks for the help guys. Much appreciated!
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2012
    Posts
    150
    Rep Power
    3
    b.t.w. how do I give you guys rewards "points?"

    When i click on the icon there to give points, I only see a zero in the selection box. I read the FAQ's on this, I believe here: http://forums.devshed.com/forum-rule...59429.html#rcs
    but I'm not finding any info on why an actual point number can't be given.

    Comments on this post

    • requinix agrees : back in the green
  10. #6
  11. SQL Consultant
    Devshed Supreme Being (6500+ posts)

    Join Date
    Feb 2003
    Location
    Toronto Canada
    Posts
    27,120
    Rep Power
    4274
    Originally Posted by we5inelgr
    but I'm not finding any info on why an actual point number can't be given.
    well, you're on the right track, that dropdown number should go as high as your current reputation
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
  12. #7
  13. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,965
    Rep Power
    9397
    Until just now you had a negative "reputation score" and that influences how much you can give. Now you're back into the positive numbers at +60... but you can only give about 1% of what you have so you might still be limited to 0.

    The whole thing doesn't really matter anyways so don't worry about it.
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2012
    Posts
    150
    Rep Power
    3
    Oh geez. lol.

    Ok, thanks.

IMN logo majestic logo threadwatch logo seochat tools logo