SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot
    Join Date
    Jan 2006
    Posts
    190
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Insert from select

    I have a database holding some default data depending on a variable. I want to create a query which copies the default data in to another table as well as insert some other data

    eg.
    INSERT table_1 (id, value1, value2)
    SELECT value1, value2 FROM table_2 WHERE group = '1',id=2

    Is it possible in one statement? Or will I have to do this as two statements?

  2. #2
    SitePoint Addict svcghost's Avatar
    Join Date
    Oct 2010
    Posts
    288
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes. But what are you trying there in your example? Why is there a comma and then "id=2" ? Is that part of the WHERE clause? It should be WHERE group='1' AND Id=2

    But let me get this straight.. You want to INSERT data from a table_2 into another table_1, but also want to INSERT data from another source into table_1 too. Right?

    Where is this other data coming from?

  3. #3
    SitePoint Zealot
    Join Date
    Jan 2006
    Posts
    190
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am creating a new entry, which gives me a new id
    I then want to create a second entry with the same id, but with data from another table.

    I will have the id saved as a variable in my php script, so it could just come from that

  4. #4
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,508
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Code:
    INSERT table_1 (id, value1, value2)
    SELECT 
        2
      , value1
      , value2 
    FROM table_2 
    WHERE group = '1'


Bookmarks

Posting Permissions

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