473,703 Members | 4,024 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

I need help with selecting from 2 identical tables in 2 separate databases

MySQL 3.23.58 - 4.0.17 (yep, several database server instances, don't
ask)

I have database Spring with table Students

I have database Summer with table Students

I am tasked to produce a query of all students in both tables with no
duplicates. No clue whatsoever.

I'm thinking an INTERSECT followed by 2 UNION SELECT statements, but
how far off am I? The tables are absolutely identical in every way
including indexes; the data MIGHT be identical (same data might be in
Spring.Students and Summer.Students ), however, that won't always be the
case. The column Spring.Students .id will not share the same value as
Summer.Students .id as data can be entered within separate sequences
even if the data in both tables is absolutely identical.

Am I on the right track with INTERSECT/UNION or what do you recommend?

Thanx
Phil

Nov 23 '05 #1
48 3861
ph************* *@gmail.com wrote:
MySQL 3.23.58 - 4.0.17 (yep, several database server instances, don't
ask)

I have database Spring with table Students

I have database Summer with table Students

I am tasked to produce a query of all students in both tables with no
duplicates. No clue whatsoever.

I'm thinking an INTERSECT followed by 2 UNION SELECT statements, but
how far off am I? The tables are absolutely identical in every way
including indexes; the data MIGHT be identical (same data might be in
Spring.Students and Summer.Students ), however, that won't always be the
case. The column Spring.Students .id will not share the same value as
Summer.Students .id as data can be entered within separate sequences
even if the data in both tables is absolutely identical.

Am I on the right track with INTERSECT/UNION or what do you recommend?

Thanx
Phil


Well, this would be easier if you had both tables in one database. I
think it's more straightforward and portable to compare tables that
exist in the same database.

If necessary, create a third database and copy the Student tables from
Spring and Summer into the third database. You can use the mysqldump
tool to create a backup of a single table from each, then edit the two
dump files with a text editor to make sure the table names will be
distinct, then restore both dumps to the third database.

Here's one query to get the set of all students. Get all students who
exist in both Spring and Summer, then all students only in Spring, then
all students only in Summer. These three sets have no overlap.

(SELECT S1.student
FROM Spring AS S1 INNER JOIN Summer AS U1 ON S1.student = U1.student)
UNION
(SELECT S2.student
FROM Spring AS S2 LEFT OUTER JOIN Summer AS U2 ON S2.student = U2.student
WHERE U2.student IS NULL)
UNION
(SELECT U3.student
FROM Spring AS S3 RIGHT OUTER JOIN Summer AS U3 ON S3.student = U3.student
WHERE S3.student IS NULL)
ORDER BY student;

Regards,
Bill K.
Nov 23 '05 #2
The right thing is to use a UNION join:

select * from Spring.Students
union
select * from Summer.Students

Markus
Nov 23 '05 #3
Unfortunately that choice produced a MySQL syntax error.. I should have
mentioned we're using MySQL 3.23.58 in the current version and will
hopefully be upgrading to MySQL 4.0+ in the new version next year

Phil

Markus Popp wrote:
The right thing is to use a UNION join:

select * from Spring.Students
union
select * from Summer.Students

Markus


Nov 23 '05 #4
See below, thanx

Bill Karwin wrote:
ph************* *@gmail.com wrote:
MySQL 3.23.58 - 4.0.17 (yep, several database server instances, don't
ask)

I have database Spring with table Students

I have database Summer with table Students

I am tasked to produce a query of all students in both tables with no
duplicates. No clue whatsoever.

I'm thinking an INTERSECT followed by 2 UNION SELECT statements, but
how far off am I? The tables are absolutely identical in every way
including indexes; the data MIGHT be identical (same data might be in
Spring.Students and Summer.Students ), however, that won't always be the
case. The column Spring.Students .id will not share the same value as
Summer.Students .id as data can be entered within separate sequences
even if the data in both tables is absolutely identical.

Am I on the right track with INTERSECT/UNION or what do you recommend?

Thanx
Phil

Well, this would be easier if you had both tables in one database. I
think it's more straightforward and portable to compare tables that
exist in the same database.


Sadly, that is not an option. The client requested they be put into
separate databases. The explanation was about as "non-techie" as it
gets so I'll just spare you, but they plan to "rejoin" the tables into
one table in the new version of the application next year
If necessary, create a third database and copy the Student tables from
Spring and Summer into the third database. You can use the mysqldump
tool to create a backup of a single table from each, then edit the two
dump files with a text editor to make sure the table names will be
distinct, then restore both dumps to the third database.

The only problem with this approach is that the separate databases are
being currently used right now in a series of web application "patch"
scripts that were written in the zero-hour to placate the customer's
need to see the students separated by "spring" and "summer", so I
cannot yet rejoin them until the customer blesses the workflow
methodology of the updated web application, which won't be until next
year..
Here's one query to get the set of all students. Get all students who
exist in both Spring and Summer, then all students only in Spring, then
all students only in Summer. These three sets have no overlap.

(SELECT S1.student
FROM Spring AS S1 INNER JOIN Summer AS U1 ON S1.student = U1.student)
UNION
(SELECT S2.student
FROM Spring AS S2 LEFT OUTER JOIN Summer AS U2 ON S2.student = U2.student
WHERE U2.student IS NULL)
UNION
(SELECT U3.student
FROM Spring AS S3 RIGHT OUTER JOIN Summer AS U3 ON S3.student = U3.student
WHERE S3.student IS NULL)
ORDER BY student;

Yeah I tried the UNION statement but it failed due to a MySQL syntax
error. I forgot to mention that we're using MySQL 3.23.58 right now
and upgrading is "not an option at this time" (yep, next year.. blah
blah blah)
Regards,
Bill K.


*sigh* Can't use UNION so don't know what to do at this point

Phil

Nov 23 '05 #5
Ok, after re-reading (and getting my second cup of coffee) my brain
just kicked into gear and realized what you just said. In short, Phil
has left <stupid&g t; mode.

That sounds doable, however, I will have to work on the means of
ensuring that the data from Spring.Students doesn't produce dups with
Summer.Students , and because UNION is not supported in MySQL 3.23.58
(I've tried to use it and consistently got syntax errors), I have
absolutely no idea how to do this :(

Phil

Bill Karwin wrote:
If necessary, create a third database and copy the Student tables from
Spring and Summer into the third database. You can use the mysqldump
tool to create a backup of a single table from each, then edit the two
dump files with a text editor to make sure the table names will be
distinct, then restore both dumps to the third database.


Nov 23 '05 #6
Incidentally, I tried your query, and, unfortunately, it produces a
syntax error in MySQL 3.23.58, which is really unfortunate considering
it's the easiest way to do this, and, at this point, don't have any
alternatives. :(

Phil
Bill Karwin wrote:
(SELECT S1.student
FROM Spring AS S1 INNER JOIN Summer AS U1 ON S1.student = U1.student)
UNION
(SELECT S2.student
FROM Spring AS S2 LEFT OUTER JOIN Summer AS U2 ON S2.student = U2.student
WHERE U2.student IS NULL)
UNION
(SELECT U3.student
FROM Spring AS S3 RIGHT OUTER JOIN Summer AS U3 ON S3.student = U3.student
WHERE S3.student IS NULL)
ORDER BY student;

Regards,
Bill K.


Nov 23 '05 #7
I'm afraid, it's not possible to do this with MySQL 3.23. You could only
query the tables separately and combine them on the client side.

If you consider updating (in fact, it's certainly a good choice to update),
I would recommand that you update straight to 5.0.x. Of course, there are
many details to take care of, but the manual stresses everything you need to
know to update from each release to another.

Markus
Nov 23 '05 #8
Hold on - there's one thing I forgot, there maybe is a solution.

If you use MyISAM tables you can use MERGE tables to combine 2 tables which
are identical in its structure. I'm not absolutely sure if MERGE tables are
supported in MySQL 3.23 (but I think so) and if it's possible to combine 2
tables that are in different databases. But in general, it works like this:

create table table_name ([column_definiti on as in the tables]) engine=merge
union=(table1, table2, ...)

Then this new table holds all records that are in each of the tables and you
can query them together.

Markus
Nov 23 '05 #9
Hmm.. I had no idea about that one, but this worked for me:

CREATE TABLE blah SELECT DISTINCT s.* FROM db1.table1 s LEFT OUTER JOIN
db2.table1 u WHERE lower(s.first_n ame) <> lower(u.first_n ame) AND
lower(s.last_na me) <> lower(u.last_na me)

Phil

Markus Popp wrote:
Hold on - there's one thing I forgot, there maybe is a solution.

If you use MyISAM tables you can use MERGE tables to combine 2 tables which
are identical in its structure. I'm not absolutely sure if MERGE tables are
supported in MySQL 3.23 (but I think so) and if it's possible to combine 2
tables that are in different databases. But in general, it works like this:

create table table_name ([column_definiti on as in the tables]) engine=merge
union=(table1, table2, ...)

Then this new table holds all records that are in each of the tables and you
can query them together.

Markus


Nov 23 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

39
2899
by: Scotter | last post by:
Okay I think my title line was worded misleadingly. So here goes again. I've got quite 20 identical MDB files running on an IIS5 server. From time to time I need to go into various tables and add a field or two. It would be great if there were an application out there that could either: (a) sync all MDB designs (and/or data) designated to match one I've added some fields/tables to OR (b) go into all designated MDBs and create new...
6
6773
by: Andreas Lauffer | last post by:
I changed from Access97 to AccessXP and I have immense performance problems. Details: - Access XP MDB with Jet 4.0 ( no ADP-Project ) - Linked Tables to SQL-Server 2000 over ODBC I used the SQL Profile to watch the T-SQL-Command which Access ( who creates the commands?) creates and noticed:
19
4103
by: James Fortune | last post by:
I have a lot of respect for David Fenton and Allen Browne, but I don't understand why people who know how to write code to completely replace a front end do not write something that will automate the code that implements managing unbound controls on forms given the superior performance of unbound controls in a client/server environment. I can easily understand a newbie using bound controls or someone with a tight deadline. I guess I need...
2
4946
by: Reidar Jorgensen | last post by:
I have several Access databases, identical in structure, but different data. Is there an easy way to combine them all into one database? There are six tables, I just want the data from all databases merged into one database. Thanks in advance.
15
4627
by: Cheryl Langdon | last post by:
Hello everyone, This is my first attempt at getting help in this manner. Please forgive me if this is an inappropriate request. I suddenly find myself in urgent need of instruction on how to communicate with a MySQL database table on a web server, from inside of my company's Access-VBA application. I know VBA pretty well but have never before needed to do this HTTP/XML/MySQL type functions.
3
2001
by: Veeru71 | last post by:
We have got 2 DB2-UDB databases (DB1 & DB2) running on separate instances( Inst1 & Inst2). DB1 has got Schema1 and DB2 has got Schema2. We would like to setup some kind of replication to replicate both Schema1 & Schema2 onto a 3 rd database (DB3) running on a 3 rd instance (Inst3). This is basically to separate out Data Reads (Reports module of the app) from the primary databases for performace reasons. Also, Some of the Reports join...
1
7109
by: =?ISO-8859-1?Q?Lasse_V=E5gs=E6ther_Karlsen?= | last post by:
I get the above error in some of the ASP.NET web applications on a server, and I need some help figuring out how to deal with it. This is a rather long post, and I hope I have enough details that someone who bothers to read all of it have some pointers. Note, I have posted the stack trace and the code exhibiting the problem further down so if you want to start by reading that, search for +++ Also note that I am unable to reproduce...
16
4850
by: Okonita via DBMonster.com | last post by:
Hi all, I am comming along with all this Linus/DB2/scripting business...I am no longer scared of it!! (LOL). But, I need to create a .ksh script that does a REORGCHK and output only tables recommended for reorg. My goal is to reorgchk and run reorgs based on entries in this reorg file as shown in the example below. I have tried my hand at the following failing script and hope that gurus here can throw me a lifeline of examples on how to...
0
9262
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9018
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8970
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7876
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6595
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5923
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4690
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2463
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2070
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.