Results 1 to 3 of 3
  1. #1
    Join Date
    Jul 2010
    Posts
    5

    Unanswered: SQL Query Copy Columns

    HI

    One user registers, his name goes to one table several times like this

    table 1

    user | categories
    name
    name
    name
    name
    name
    name

    (column user value "name" is the same for all rows)

    then in another table i have the same number of categories as the name values inserted

    table 2

    categories
    cat1
    cat2
    cat3
    cat4
    cat5
    cat6

    I want to UPDATE table one and insert each category to the name and the result would be

    table 1
    name | category
    name cat1
    name cat2
    name cat3
    name cat4
    name cat5
    name cat6

    Is there a way to do that?
    thx

  2. #2
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    Quote Originally Posted by MrCapuchino View Post
    One user registers, his name goes to one table several times like this
    this is an unnecessary step

    instead of multiple inserts followed by an update, you can do it all in one step like this --
    Code:
    INSERT
      INTO table1
    SELECT '$user'
         , cat
      FROM categories
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  3. #3
    Join Date
    Jul 2010
    Posts
    5

    I was doing it all wrong

    Hello,

    After talking with a professional programmer he told me that I was doing everything the wrong way,

    What I want to do is best done with FOREIGN KEYS, I did not know how to use them but now I'm understanding how they work and I totally need them to do what I want in my application.

    That's why all the answers from other people did not worked because they thought I already was managing foreign keys.

    Thanks everyone for the help!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •