Any SQL gurus in the house? Can you help?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Phil
    Confirmed User
    • Jan 2004
    • 7659

    #1

    Any SQL gurus in the house? Can you help?

    I have two identical structure DBs. One has 1000+ records (DB1) the other one that has 200 (DB2). I would like to combine those two by adding 1000 records to smaller DB skipping duplicates (based on primary key - ID).. so basically moving 800 records starting with ID=201 from DB1 to DB2
    Ask Phil
  • Babaganoosh
    ♥♥♥ Likes Hugs ♥♥♥
    • Nov 2001
    • 15841

    #2
    If the primary key is unique you could do a regular insert and mysql will just throw an error and continue when it sees a duplicate id.
    I like pie.

    Comment

    • Phil
      Confirmed User
      • Jan 2004
      • 7659

      #3
      mysql will error out n that. Im using insert ingnore should work but I'm missing something there. screw it, will do it tomorrow.

      BTW, why isn't "rollback" command working in MySql?
      Ask Phil

      Comment

      • GrouchyAdmin
        Now choke yourself!
        • Apr 2006
        • 12085

        #4
        select * into db2 from db1 where (id <= 201 and id >= 1000)?

        Comment

        • brandonstills
          Confirmed User
          • Dec 2007
          • 1964

          #5
          1. Get the list of ids on server #1
          2. Combine them into a comma separated list: join(', ', $theList);
          3. "SELECT * FROM table WHERE id NOT IN ($theList)"

          Brandon Stills
          Industry and programming veteran
          [email protected] | skype: brandonstills | ICQ #495-171-318

          Comment

          • Varius
            Confirmed User
            • Jun 2004
            • 6890

            #6
            Originally posted by GrouchyAdmin
            select * into db2 from db1 where (id <= 201 and id >= 1000)?
            Won't work if the IDs that are in one but not the other aren't sequential

            Have to use a join (right, left, outer, whatever way you prefer hehe) in that case.

            EDIT: My bad, he did say they are starting with 201 heh. So nevermind.
            Last edited by Varius; 09-24-2008, 06:15 PM.
            Skype variuscr - Email varius AT gmail

            Comment

            • gornyhuy
              Chafed.
              • May 2002
              • 18041

              #7
              http://bogdan.org.ua/2007/10/18/mysq...ts-syntax.html

              icq:159548293

              Comment

              • pstation
                Confirmed User
                • Jul 2003
                • 1135

                #8
                Originally posted by CAMOKAT
                mysql will error out n that. Im using insert ingnore should work but I'm missing something there. screw it, will do it tomorrow.

                BTW, why isn't "rollback" command working in MySql?
                are you using INNODB? myisam does not support transactions

                Comment

                • sarettah
                  see you later, I'm gone
                  • Oct 2002
                  • 14351

                  #9
                  INSERT INTO db2 SELECT * FROM db1 WHERE id NOT IN (SELECT id FROM db2)
                  All cookies cleared!

                  Comment

                  • Lane
                    Will code for food...
                    • Apr 2001
                    • 8496

                    #10
                    why didn't "insert ignore" work?

                    Comment

                    • mikesouth
                      Confirmed User
                      • Jun 2003
                      • 6340

                      #11
                      im not sure I see the problem a simple unload of one database to a file then insert it into the other. If you want more control unload both to flat files, merge and sort (UNIX) then and use the uniq command(UNIX) to dedupe the merged file then load it into a database
                      Mike South

                      It's No wonder I took up drugs and alcohol, it's the only way I could dumb myself down enough to cope with the morons in this biz.

                      Comment

                      • GigoloShawn
                        Confirmed User
                        • Oct 2007
                        • 700

                        #12
                        Originally posted by mikesouth
                        im not sure I see the problem a simple unload of one database to a file then insert it into the other. If you want more control unload both to flat files, merge and sort (UNIX) then and use the uniq command(UNIX) to dedupe the merged file then load it into a database
                        Well, that's one way to entirely avoid the hassle of, uh, the database part.
                        I no longer represent TrafficGigolos, please contact Justin or Rebecca with any issues.

                        Comment

                        • Owen Pierce
                          Registered User
                          • Dec 2007
                          • 95

                          #13
                          uhm... you said "DB1" and "DB2".. so.. I'm gonna assume you actually mean databases, although it seems more likely you mean tables... anyways.

                          INSERT INTO
                          db1.table
                          (field1,field2,field3)
                          SELECT
                          t2.field1,
                          t2.field2,
                          t2.field3
                          FROM
                          db1.table t2
                          LEFT OUTER JOIN db2.table t1 ON (t1.id = t2.id)
                          WHERE
                          t1.id IS NULL
                          Models/Agency search: http://strongthread.com

                          Comment

                          • Owen Pierce
                            Registered User
                            • Dec 2007
                            • 95

                            #14
                            Originally posted by sarettah
                            INSERT INTO db2 SELECT * FROM db1 WHERE id NOT IN (SELECT id FROM db2)
                            sub-selects? YIKES!
                            Models/Agency search: http://strongthread.com

                            Comment

                            • Owen Pierce
                              Registered User
                              • Dec 2007
                              • 95

                              #15
                              Originally posted by mikesouth
                              im not sure I see the problem a simple unload of one database to a file then insert it into the other. If you want more control unload both to flat files, merge and sort (UNIX) then and use the uniq command(UNIX) to dedupe the merged file then load it into a database
                              that doesn't work if he's in INNODB...
                              and uhm... doing some file based process to fix database issues is just asking for problems.

                              not to mention, db files are binary... he's not doing a local RSYNC or something...

                              Models/Agency search: http://strongthread.com

                              Comment

                              • GrouchyAdmin
                                Now choke yourself!
                                • Apr 2006
                                • 12085

                                #16
                                Originally posted by Owen Pierce
                                that doesn't work if he's in INNODB...
                                and uhm... doing some file based process to fix database issues is just asking for problems.

                                not to mention, db files are binary... he's not doing a local RSYNC or something...

                                I pray to god he was at least assuming doing two dumps.. but the whole idea of doing that just has to be tongue in cheek.

                                Comment

                                • mrkris
                                  Confirmed User
                                  • May 2005
                                  • 2737

                                  #17
                                  You all make shit way to complex.

                                  PHP-MySQL-Rails | ICQ: 342500546

                                  Comment

                                  • mikesouth
                                    Confirmed User
                                    • Jun 2003
                                    • 6340

                                    #18
                                    Originally posted by Owen Pierce
                                    that doesn't work if he's in INNODB...
                                    and uhm... doing some file based process to fix database issues is just asking for problems.

                                    not to mention, db files are binary... he's not doing a local RSYNC or something...

                                    nah its a cakewalk and way faster than cross database transfers assuming his dbms even allows such things

                                    I also assumed he really meant databases and not relations (tables)

                                    It mostly just a matter of understanding your data dbload and dbunload seem simplest.
                                    Mike South

                                    It's No wonder I took up drugs and alcohol, it's the only way I could dumb myself down enough to cope with the morons in this biz.

                                    Comment

                                    • GrouchyAdmin
                                      Now choke yourself!
                                      • Apr 2006
                                      • 12085

                                      #19
                                      Originally posted by mrkris
                                      You all make shit way to complex.
                                      You smell like Grandma.

                                      Comment

                                      • Phil
                                        Confirmed User
                                        • Jan 2004
                                        • 7659

                                        #20
                                        I had 4 beers tonight, so Im not touching it until tomorrow, but please tell me why "rollback" isn't working for MySql. Is it oracle only feature? does it do commit on it own???
                                        Last edited by Phil; 09-24-2008, 11:04 PM.
                                        Ask Phil

                                        Comment

                                        • Phil
                                          Confirmed User
                                          • Jan 2004
                                          • 7659

                                          #21
                                          Originally posted by Owen Pierce
                                          uhm... you said "DB1" and "DB2".. so.. I'm gonna assume you actually mean databases, although it seems more likely you mean tables... anyways.

                                          INSERT INTO
                                          db1.table
                                          (field1,field2,field3)
                                          SELECT
                                          t2.field1,
                                          t2.field2,
                                          t2.field3
                                          FROM
                                          db1.table t2
                                          LEFT OUTER JOIN db2.table t1 ON (t1.id = t2.id)
                                          WHERE
                                          t1.id IS NULL

                                          databases.. each has identical structure.. I want to combine entries...
                                          Ask Phil

                                          Comment

                                          • PPC
                                            Registered User
                                            • Jun 2007
                                            • 60

                                            #22
                                            export both into excel

                                            and select all data then goto toolbar Data > Filter > Advanced Filter

                                            click copy to another location change the copy to range ... click unique records only then hit ok

                                            but hey, I'm no SQL guru :p

                                            (btw this will only work for up to like 65k, excels row limit)
                                            Last edited by PPC; 09-24-2008, 11:10 PM.
                                            Pay Per Click

                                            Comment

                                            • Phil
                                              Confirmed User
                                              • Jan 2004
                                              • 7659

                                              #23
                                              Originally posted by PPC
                                              export both into excel

                                              and select all data then goto toolbar Data > Filter > Advanced Filter

                                              click copy to another location change the copy to range ... click unique records only then hit ok

                                              but hey, I'm no SQL guru :p

                                              (btw this will only work for up to like 65k, excels row limit)


                                              thanks man...

                                              Ask Phil

                                              Comment

                                              • GrouchyAdmin
                                                Now choke yourself!
                                                • Apr 2006
                                                • 12085

                                                #24
                                                Or, ya know, you could just take my query and insert the field/values but NOT the auto_increment key field for X records.

                                                But, hey, I haven't seen so many convoluted ways to do database transactions OUTSIDE of a database in.. forever.

                                                Comment

                                                • rowan
                                                  Too lazy to set a custom title
                                                  • Mar 2002
                                                  • 17393

                                                  #25
                                                  Originally posted by GrouchyAdmin
                                                  Or, ya know, you could just take my query and insert the field/values but NOT the auto_increment key field for X records.

                                                  But, hey, I haven't seen so many convoluted ways to do database transactions OUTSIDE of a database in.. forever.
                                                  A CSV file still has "data" in it! :D

                                                  Comment

                                                  • budz
                                                    Disruptive Innovator
                                                    • Sep 2003
                                                    • 4230

                                                    #26
                                                    Originally posted by rowan
                                                    A CSV file still has "data" in it! :D
                                                    funny.. I was thinking something like that about excel

                                                    http://spreadsheets.about.com/od/dat...l_database.htm
                                                    C:\Code\
                                                    C:\Code\Run\

                                                    Comment

                                                    • V_RocKs
                                                      Damn Right I Kiss Ass!
                                                      • Nov 2003
                                                      • 32451

                                                      #27
                                                      Or just select all from the 1000 and select all from 200 where id = id and if it don't match, insert

                                                      Comment

                                                      • sarettah
                                                        see you later, I'm gone
                                                        • Oct 2002
                                                        • 14351

                                                        #28
                                                        INSERT INTO db2 SELECT * FROM db1 WHERE id NOT IN (SELECT id FROM db2)
                                                        Originally posted by Owen Pierce
                                                        sub-selects? YIKES!

                                                        Yep. Pretty quick, simple and straight forward.

                                                        And your issue with sub-selects is ??
                                                        All cookies cleared!

                                                        Comment

                                                        • grumpy
                                                          Too lazy to set a custom title
                                                          • Jan 2002
                                                          • 9870

                                                          #29
                                                          some real geniuses here
                                                          Don't let greediness blur your vision | You gotta let some shit slide
                                                          icq - 441-456-888

                                                          Comment

                                                          • Owen Pierce
                                                            Registered User
                                                            • Dec 2007
                                                            • 95

                                                            #30
                                                            Originally posted by CAMOKAT
                                                            databases.. each has identical structure.. I want to combine entries...
                                                            well if the tables in each DB are the same.. then..

                                                            INSERT INTO
                                                            db1.table
                                                            (*)
                                                            SELECT
                                                            t2.*
                                                            FROM
                                                            db1.table t2
                                                            LEFT OUTER JOIN db2.table t1 ON (t1.id = t2.id)
                                                            WHERE
                                                            t1.id IS NULL


                                                            just make sure the permissions of the user you are runnign this as has access to both db's.
                                                            Models/Agency search: http://strongthread.com

                                                            Comment

                                                            • Owen Pierce
                                                              Registered User
                                                              • Dec 2007
                                                              • 95

                                                              #31
                                                              Originally posted by sarettah
                                                              Yep. Pretty quick, simple and straight forward.

                                                              And your issue with sub-selects is ??
                                                              they are super slow, bad practice. I wish MySQL never put that damn functionality into itself.

                                                              when I have to fix other peoples legacy code.. finding queries that take 14 seconds for no reason at all.. it's usually due to sub-selects.
                                                              OR bad index's...
                                                              but most sub-selects
                                                              Models/Agency search: http://strongthread.com

                                                              Comment

                                                              • Owen Pierce
                                                                Registered User
                                                                • Dec 2007
                                                                • 95

                                                                #32
                                                                Originally posted by mikesouth
                                                                nah its a cakewalk and way faster than cross database transfers assuming his dbms even allows such things

                                                                I also assumed he really meant databases and not relations (tables)

                                                                It mostly just a matter of understanding your data dbload and dbunload seem simplest.
                                                                quicker?
                                                                hehe, obviously you're a *nix guy and not a DBA...
                                                                Models/Agency search: http://strongthread.com

                                                                Comment

                                                                • sarettah
                                                                  see you later, I'm gone
                                                                  • Oct 2002
                                                                  • 14351

                                                                  #33
                                                                  Originally posted by Owen Pierce
                                                                  they are super slow, bad practice. I wish MySQL never put that damn functionality into itself.

                                                                  when I have to fix other peoples legacy code.. finding queries that take 14 seconds for no reason at all.. it's usually due to sub-selects.
                                                                  OR bad index's...
                                                                  but most sub-selects
                                                                  Yeah, but this sounds like he wanted something for one time use not something that was going to be running in an ongoing process.

                                                                  When it is one time use take the easiest route.
                                                                  All cookies cleared!

                                                                  Comment

                                                                  • mikesouth
                                                                    Confirmed User
                                                                    • Jun 2003
                                                                    • 6340

                                                                    #34
                                                                    Originally posted by Owen Pierce
                                                                    quicker?
                                                                    hehe, obviously you're a *nix guy and not a DBA...
                                                                    actually I was a sr consultant for Informix for yrs but ya I got lots of UNIX too thing is my informix days were back in 1993 and I havent really kept up with RDBMS since...
                                                                    Mike South

                                                                    It's No wonder I took up drugs and alcohol, it's the only way I could dumb myself down enough to cope with the morons in this biz.

                                                                    Comment

                                                                    • Varius
                                                                      Confirmed User
                                                                      • Jun 2004
                                                                      • 6890

                                                                      #35
                                                                      You could always use INSERT IGNORE also, to just try and insert them all from db1 and it will error on any duplicates (primary key), BUT keep going and not stop if you are using INSERT IGNORE
                                                                      Skype variuscr - Email varius AT gmail

                                                                      Comment

                                                                      • react
                                                                        Confirmed User
                                                                        • Sep 2003
                                                                        • 673

                                                                        #36
                                                                        Yes, stop fucking stringing us along and tell us why INSERT IGNORE is not an option.
                                                                        --
                                                                        react

                                                                        Comment

                                                                        • Owen Pierce
                                                                          Registered User
                                                                          • Dec 2007
                                                                          • 95

                                                                          #37
                                                                          Originally posted by mikesouth
                                                                          actually I was a sr consultant for Informix for yrs but ya I got lots of UNIX too thing is my informix days were back in 1993 and I havent really kept up with RDBMS since...
                                                                          either way, you're way doesn't work...
                                                                          cuz if he's using INNODB, it can't be done outside of SQL.
                                                                          if he's using INNODB then he wouldn't need any sort of comparison, he could just flat out copy the file from one DB to the other.
                                                                          AND that's all assuming he has access to the server, not just SQL access. AND that's assuming he knows how to navigate around *nix at all.
                                                                          Models/Agency search: http://strongthread.com

                                                                          Comment

                                                                          • Owen Pierce
                                                                            Registered User
                                                                            • Dec 2007
                                                                            • 95

                                                                            #38
                                                                            Originally posted by sarettah
                                                                            Yeah, but this sounds like he wanted something for one time use not something that was going to be running in an ongoing process.

                                                                            When it is one time use take the easiest route.
                                                                            It's still poison. ;)
                                                                            Models/Agency search: http://strongthread.com

                                                                            Comment

                                                                            • Owen Pierce
                                                                              Registered User
                                                                              • Dec 2007
                                                                              • 95

                                                                              #39
                                                                              Shouldn't this whole thing been in the webmaster forum anyways? heheh
                                                                              Models/Agency search: http://strongthread.com

                                                                              Comment

                                                                              • Owen Pierce
                                                                                Registered User
                                                                                • Dec 2007
                                                                                • 95

                                                                                #40
                                                                                Anyways.. COMKAT, there are like 3 solutions in here...

                                                                                pick one of them.
                                                                                say thanks.
                                                                                close the thread.
                                                                                Models/Agency search: http://strongthread.com

                                                                                Comment

                                                                                • Phil
                                                                                  Confirmed User
                                                                                  • Jan 2004
                                                                                  • 7659

                                                                                  #41
                                                                                  Ok, figured it out. The reason insert ignore didn't work is because when I did export into sql file through phpMyAdmin, it added extra line with ; in it ... don't know why.
                                                                                  so thank you, and its closed.
                                                                                  Ask Phil

                                                                                  Comment

                                                                                  • react
                                                                                    Confirmed User
                                                                                    • Sep 2003
                                                                                    • 673

                                                                                    #42
                                                                                    Forget the export..

                                                                                    insert ignore into table2 select * from table1;

                                                                                    or if you have a disparate structure:

                                                                                    insert ignore into table2 (some, fields, here) select some, fields, here from table1;

                                                                                    Glad it worked out.
                                                                                    --
                                                                                    react

                                                                                    Comment

                                                                                    Working...