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
Any SQL gurus in the house? Can you help?
Collapse
X
-
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. -
-
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)"Comment
-
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 gmailComment
-
-
Comment
-
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 databaseMike 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
-
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
-
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 NULLModels/Agency search: http://strongthread.comComment
-
-
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.comComment
-
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
-
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
-
Comment
-
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...Comment
-
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
-
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...
Comment
-
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
-
A CSV file still has "data" in it! :DComment
-
funny.. I was thinking something like that about excel
http://spreadsheets.about.com/od/dat...l_database.htmC:\Code\
C:\Code\Run\Comment
-
Comment
-
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.comComment
-
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-selectsModels/Agency search: http://strongthread.comComment
-
quicker?
hehe, obviously you're a *nix guy and not a DBA...Models/Agency search: http://strongthread.comComment
-
Yeah, but this sounds like he wanted something for one time use not something that was going to be running in an ongoing process.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
When it is one time use take the easiest route.All cookies cleared!Comment
-
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
-
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.comComment
-
-
Shouldn't this whole thing been in the webmaster forum anyways? hehehModels/Agency search: http://strongthread.comComment
-
Anyways.. COMKAT, there are like 3 solutions in here...
pick one of them.
say thanks.
close the thread.
Models/Agency search: http://strongthread.comComment

AIM: GrouchyGfy


Comment