|
|
|
||||
|
Welcome to the GoFuckYourself.com - Adult Webmaster Forum forums. You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today! If you have any problems with the registration process or your account login, please contact us. |
![]() |
|
|||||||
| Discuss what's fucking going on, and which programs are best and worst. One-time "program" announcements from "established" webmasters are allowed. |
|
|
Thread Tools |
|
|
#1 |
|
Confirmed User
Join Date: Jan 2004
Posts: 7,659
|
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 |
|
|
|
|
|
#2 |
|
♥♥♥ Likes Hugs ♥♥♥
Industry Role:
Join Date: Nov 2001
Location: /home
Posts: 15,841
|
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. |
|
|
|
|
|
#4 |
|
Now choke yourself!
Industry Role:
Join Date: Apr 2006
Posts: 12,085
|
select * into db2 from db1 where (id <= 201 and id >= 1000)?
__________________
|
|
|
|
|
|
#5 |
|
Confirmed User
Join Date: Dec 2007
Location: Chatsworth, CA
Posts: 1,964
|
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)" |
|
|
|
|
|
#6 | |
|
Confirmed User
Industry Role:
Join Date: Jun 2004
Location: New York, NY
Posts: 6,890
|
Quote:
![]() 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.
__________________
Skype variuscr - Email varius AT gmail |
|
|
|
|
|
|
#7 |
|
Chafed.
Join Date: May 2002
Location: Face Down in Pussy
Posts: 18,041
|
|
|
|
|
|
|
#8 |
|
Confirmed User
Join Date: Jul 2003
Location: chicago
Posts: 1,135
|
|
|
|
|
|
|
#9 |
|
see you later, I'm gone
Industry Role:
Join Date: Oct 2002
Posts: 14,127
|
INSERT INTO db2 SELECT * FROM db1 WHERE id NOT IN (SELECT id FROM db2)
__________________
All cookies cleared! |
|
|
|
|
|
#10 |
|
Will code for food...
Join Date: Apr 2001
Location: Buckeye, AZ
Posts: 8,496
|
why didn't "insert ignore" work?
__________________
|
|
|
|
|
|
#11 |
|
Confirmed User
Industry Role:
Join Date: Jun 2003
Location: My High Horse
Posts: 6,334
|
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. |
|
|
|
|
|
#12 | |
|
Confirmed User
Join Date: Oct 2007
Location: No longer with Star Marketing Group.
Posts: 700
|
Quote:
__________________
I no longer represent TrafficGigolos, please contact Justin or Rebecca with any issues.
|
|
|
|
|
|
|
#13 |
|
Registered User
Join Date: Dec 2007
Posts: 95
|
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 |
|
|
|
|
|
#14 |
|
Registered User
Join Date: Dec 2007
Posts: 95
|
|
|
|
|
|
|
#15 | |
|
Registered User
Join Date: Dec 2007
Posts: 95
|
Quote:
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... |
|
|
|
|
|
|
#16 |
|
Now choke yourself!
Industry Role:
Join Date: Apr 2006
Posts: 12,085
|
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.
__________________
|
|
|
|
|
|
#17 |
|
Confirmed User
Join Date: May 2005
Posts: 2,737
|
You all make shit way to complex.
|
|
|
|
|
|
#18 | |
|
Confirmed User
Industry Role:
Join Date: Jun 2003
Location: My High Horse
Posts: 6,334
|
Quote:
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. |
|
|
|
|
|
|
#19 |
|
Now choke yourself!
Industry Role:
Join Date: Apr 2006
Posts: 12,085
|
__________________
|
|
|
|
|
|
#21 | |
|
Confirmed User
Join Date: Jan 2004
Posts: 7,659
|
Quote:
databases.. each has identical structure.. I want to combine entries...
__________________
Ask Phil |
|
|
|
|
|
|
#22 |
|
Registered User
Join Date: Jun 2007
Posts: 60
|
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)
__________________
Pay Per Click
|
|
|
|
|
|
#23 | |
|
Confirmed User
Join Date: Jan 2004
Posts: 7,659
|
Quote:
![]() ![]() thanks man... ![]()
__________________
Ask Phil |
|
|
|
|
|
|
#24 |
|
Now choke yourself!
Industry Role:
Join Date: Apr 2006
Posts: 12,085
|
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.
__________________
|
|
|
|
|
|
#25 |
|
Too lazy to set a custom title
Join Date: Mar 2002
Location: Australia
Posts: 17,393
|
A CSV file still has "data" in it! :D
|
|
|
|
|
|
#26 |
|
Disruptive Innovator
Industry Role:
Join Date: Sep 2003
Location: Vegas
Posts: 4,230
|
funny.. I was thinking something like that about excel
http://spreadsheets.about.com/od/dat...l_database.htm
__________________
C:\Code\ C:\Code\Run\ |
|
|
|
|
|
#27 |
|
Damn Right I Kiss Ass!
Industry Role:
Join Date: Dec 2003
Location: Cowtown, USA
Posts: 32,422
|
Or just select all from the 1000 and select all from 200 where id = id and if it don't match, insert
|
|
|
|
|
|
#28 | |
|
see you later, I'm gone
Industry Role:
Join Date: Oct 2002
Posts: 14,127
|
Quote:
Yep. Pretty quick, simple and straight forward. And your issue with sub-selects is ??
__________________
All cookies cleared! |
|
|
|
|
|
|
#29 |
|
Too lazy to set a custom title
Join Date: Jan 2002
Location: Holland
Posts: 9,870
|
some real geniuses here
__________________
Don't let greediness blur your vision | You gotta let some shit slide icq - 441-456-888 |
|
|
|
|
|
#30 | |
|
Registered User
Join Date: Dec 2007
Posts: 95
|
Quote:
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. |
|
|
|
|
|
|
#31 | |
|
Registered User
Join Date: Dec 2007
Posts: 95
|
Quote:
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 |
|
|
|
|
|
|
#32 | |
|
Registered User
Join Date: Dec 2007
Posts: 95
|
Quote:
hehe, obviously you're a *nix guy and not a DBA... |
|
|
|
|
|
|
#33 | |
|
see you later, I'm gone
Industry Role:
Join Date: Oct 2002
Posts: 14,127
|
Quote:
When it is one time use take the easiest route.
__________________
All cookies cleared! |
|
|
|
|
|
|
#34 |
|
Confirmed User
Industry Role:
Join Date: Jun 2003
Location: My High Horse
Posts: 6,334
|
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. |
|
|
|
|
|
#35 |
|
Confirmed User
Industry Role:
Join Date: Jun 2004
Location: New York, NY
Posts: 6,890
|
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 |
|
|
|
|
|
#36 |
|
Confirmed User
Industry Role:
Join Date: Sep 2003
Location: NZ
Posts: 673
|
Yes, stop fucking stringing us along and tell us why INSERT IGNORE is not an option.
__________________
-- react |
|
|
|
|
|
#37 | |
|
Registered User
Join Date: Dec 2007
Posts: 95
|
Quote:
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. |
|
|
|
|
|
|
#38 |
|
Registered User
Join Date: Dec 2007
Posts: 95
|
|
|
|
|
|
|
#39 |
|
Registered User
Join Date: Dec 2007
Posts: 95
|
Shouldn't this whole thing been in the webmaster forum anyways? heheh
|
|
|
|
|
|
#40 |
|
Registered User
Join Date: Dec 2007
Posts: 95
|
Anyways.. COMKAT, there are like 3 solutions in here...
pick one of them. say thanks. close the thread. |
|
|
|
|
|
#41 |
|
Confirmed User
Join Date: Jan 2004
Posts: 7,659
|
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 |
|
|
|
|
|
#42 |
|
Confirmed User
Industry Role:
Join Date: Sep 2003
Location: NZ
Posts: 673
|
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 |
|
|
|