Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. How can I add the data found from one table to another?

How can I add the data found from one table to another?

Scheduled Pinned Locked Moved Database
questiondatabasemysqldata-structureshelp
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    djhankypark
    wrote on last edited by
    #1

    Hi I am comparing data between 2 tables looking for what exists in one so i can then enter whatis found into the second table as follows...

    mysql_select_db($database_Chartmenu, $Chartmenu);
    $query_find_all = "SELECT hankypark.ID FROM hankypark LEFT JOIN tbl_ply_hankypark ON hankypark.ID = tbl_ply_hankypark.Song_ID WHERE (((tbl_ply_hankypark.Song_ID) Is Null))";
    $find_all = mysql_query($query_find_all, $Chartmenu) or die(mysql_error());
    $row_find_all = mysql_fetch_assoc($find_all);
    $totalRows_find_all = mysql_num_rows($find_all);

    in the hankypark table i know will be entries that do not exist in the tbl_ply_hankypark table hense the Is Null query which gives me an array of the ID field, I now want to add those ID's to the tbl_ply_hankypark table. Question is what do I use to roll through all the ID's found ? My INSERT query looks like this...as said what do use tat the begining is it a... while or do or what?? and how should it be properly laid out please? not having played with arrays I have tried several things from suggestions on the net but none seem to work apart from one that only inserted the first ID and then stopped? // Insert a row of information into the table "example" mysql_query("INSERT INTO tbl_ply_hankypark (Song_ID) VALUES ('???')")or die(mysql_error()); Thought it best to just bite the bullet and come ask you guys cos your always right lol :) Cheers in advance of any help Regards Ray

    P P 2 Replies Last reply
    0
    • D djhankypark

      Hi I am comparing data between 2 tables looking for what exists in one so i can then enter whatis found into the second table as follows...

      mysql_select_db($database_Chartmenu, $Chartmenu);
      $query_find_all = "SELECT hankypark.ID FROM hankypark LEFT JOIN tbl_ply_hankypark ON hankypark.ID = tbl_ply_hankypark.Song_ID WHERE (((tbl_ply_hankypark.Song_ID) Is Null))";
      $find_all = mysql_query($query_find_all, $Chartmenu) or die(mysql_error());
      $row_find_all = mysql_fetch_assoc($find_all);
      $totalRows_find_all = mysql_num_rows($find_all);

      in the hankypark table i know will be entries that do not exist in the tbl_ply_hankypark table hense the Is Null query which gives me an array of the ID field, I now want to add those ID's to the tbl_ply_hankypark table. Question is what do I use to roll through all the ID's found ? My INSERT query looks like this...as said what do use tat the begining is it a... while or do or what?? and how should it be properly laid out please? not having played with arrays I have tried several things from suggestions on the net but none seem to work apart from one that only inserted the first ID and then stopped? // Insert a row of information into the table "example" mysql_query("INSERT INTO tbl_ply_hankypark (Song_ID) VALUES ('???')")or die(mysql_error()); Thought it best to just bite the bullet and come ask you guys cos your always right lol :) Cheers in advance of any help Regards Ray

      P Online
      P Online
      PIEBALDconsult
      wrote on last edited by
      #2

      Look into using an INSERT/SELECT statement.

      1 Reply Last reply
      0
      • D djhankypark

        Hi I am comparing data between 2 tables looking for what exists in one so i can then enter whatis found into the second table as follows...

        mysql_select_db($database_Chartmenu, $Chartmenu);
        $query_find_all = "SELECT hankypark.ID FROM hankypark LEFT JOIN tbl_ply_hankypark ON hankypark.ID = tbl_ply_hankypark.Song_ID WHERE (((tbl_ply_hankypark.Song_ID) Is Null))";
        $find_all = mysql_query($query_find_all, $Chartmenu) or die(mysql_error());
        $row_find_all = mysql_fetch_assoc($find_all);
        $totalRows_find_all = mysql_num_rows($find_all);

        in the hankypark table i know will be entries that do not exist in the tbl_ply_hankypark table hense the Is Null query which gives me an array of the ID field, I now want to add those ID's to the tbl_ply_hankypark table. Question is what do I use to roll through all the ID's found ? My INSERT query looks like this...as said what do use tat the begining is it a... while or do or what?? and how should it be properly laid out please? not having played with arrays I have tried several things from suggestions on the net but none seem to work apart from one that only inserted the first ID and then stopped? // Insert a row of information into the table "example" mysql_query("INSERT INTO tbl_ply_hankypark (Song_ID) VALUES ('???')")or die(mysql_error()); Thought it best to just bite the bullet and come ask you guys cos your always right lol :) Cheers in advance of any help Regards Ray

        P Offline
        P Offline
        Pete OHanlon
        wrote on last edited by
        #3

        One thing that you can do is combine an insert and a select command together, so you actually get the values inserted that you select from the select query. Your command becomes something like this:

        INSERT INTO tbl_ply_hankypark(song_id)
        SELECT hankypark.ID FROM hankypark
        LEFT JOIN tbl_ply_hankypark ON
        hankypark.ID = tbl_ply_hankypark.Song_ID
        WHERE (((tbl_ply_hankypark.Song_ID) Is Null))

        I have CDO, it's OCD with the letters in the right order; just as they ruddy well should be

        Forgive your enemies - it messes with their heads

        My blog | My articles | MoXAML PowerToys | Onyx

        D 1 Reply Last reply
        0
        • P Pete OHanlon

          One thing that you can do is combine an insert and a select command together, so you actually get the values inserted that you select from the select query. Your command becomes something like this:

          INSERT INTO tbl_ply_hankypark(song_id)
          SELECT hankypark.ID FROM hankypark
          LEFT JOIN tbl_ply_hankypark ON
          hankypark.ID = tbl_ply_hankypark.Song_ID
          WHERE (((tbl_ply_hankypark.Song_ID) Is Null))

          I have CDO, it's OCD with the letters in the right order; just as they ruddy well should be

          Forgive your enemies - it messes with their heads

          My blog | My articles | MoXAML PowerToys | Onyx

          D Offline
          D Offline
          djhankypark
          wrote on last edited by
          #4

          Thnak you for both replies I knew I would get the right directions here Many Thanks for you help Regards Ray

          P 1 Reply Last reply
          0
          • D djhankypark

            Thnak you for both replies I knew I would get the right directions here Many Thanks for you help Regards Ray

            P Offline
            P Offline
            Pete OHanlon
            wrote on last edited by
            #5

            You're welcome.

            I have CDO, it's OCD with the letters in the right order; just as they ruddy well should be

            Forgive your enemies - it messes with their heads

            My blog | My articles | MoXAML PowerToys | Onyx

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups