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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • phillip.s.powell@gmail.com

    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

  • Bill Karwin

    #2
    Re: I need help with selecting from 2 identical tables in 2 separatedatabas es

    phillip.s.powel l@gmail.com wrote:[color=blue]
    > 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
    >[/color]

    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.

    Comment

    • Markus Popp

      #3
      Re: I need help with selecting from 2 identical tables in 2 separate databases

      The right thing is to use a UNION join:

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

      Markus


      Comment

      • phillip.s.powell@gmail.com

        #4
        Re: I need help with selecting from 2 identical tables in 2 separate databases

        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:[color=blue]
        > The right thing is to use a UNION join:
        >
        > select * from Spring.Students
        > union
        > select * from Summer.Students
        >
        > Markus[/color]

        Comment

        • phillip.s.powell@gmail.com

          #5
          Re: I need help with selecting from 2 identical tables in 2 separate databases

          See below, thanx

          Bill Karwin wrote:[color=blue]
          > phillip.s.powel l@gmail.com wrote:[color=green]
          > > 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
          > >[/color]
          >
          > 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.
          >[/color]

          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
          [color=blue]
          > 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.
          >[/color]

          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..
          [color=blue]
          > 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;
          >[/color]

          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)
          [color=blue]
          > Regards,
          > Bill K.[/color]

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

          Phil

          Comment

          • phillip.s.powell@gmail.com

            #6
            Re: I need help with selecting from 2 identical tables in 2 separate databases

            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:
            [color=blue]
            > 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.
            >[/color]

            Comment

            • phillip.s.powell@gmail.com

              #7
              Re: I need help with selecting from 2 identical tables in 2 separate databases

              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:
              [color=blue]
              > (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.[/color]

              Comment

              • Markus Popp

                #8
                Re: I need help with selecting from 2 identical tables in 2 separate databases

                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


                Comment

                • Markus Popp

                  #9
                  Re: I need help with selecting from 2 identical tables in 2 separate databases

                  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


                  Comment

                  • phillip.s.powell@gmail.com

                    #10
                    Re: I need help with selecting from 2 identical tables in 2 separate databases

                    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:[color=blue]
                    > 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[/color]

                    Comment

                    • Markus Popp

                      #11
                      Re: I need help with selecting from 2 identical tables in 2 separate databases

                      > CREATE TABLE blah SELECT DISTINCT s.* FROM db1.table1 s LEFT OUTER JOIN[color=blue]
                      > 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)[/color]

                      This looks interesting - and makes sense.

                      Markus


                      Comment

                      • Bill Karwin

                        #12
                        Re: I need help with selecting from 2 identical tables in 2 separatedatabas es

                        phillip.s.powel l@gmail.com wrote:[color=blue]
                        > 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 :([/color]

                        The easiest path would be to install MySQL 4.1 on another computer (even
                        your home PC), transfer the table dumps from the client's computer to
                        yours, and then do the UNION query on MySQL 4.1. Then upload the
                        finished report of distinct students.

                        Repeat as necessary, every time they want an updated report of the
                        students. Keep track of the time spent doing these convoluted steps
                        carefully, so you can tell the client how much it is costing them to
                        _not_ have the tables in one database and up-to-date MySQL software. >:-)

                        Regards,
                        Bill K.

                        Comment

                        • Bill Karwin

                          #13
                          Re: I need help with selecting from 2 identical tables in 2 separatedatabas es

                          phillip.s.powel l@gmail.com wrote:[color=blue]
                          > 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)[/color]

                          Great work! I can never remember if a given RDBMS product & version can
                          support cross-database joins.

                          Regards,
                          Bill K.

                          Comment

                          • phillip.s.powell@gmail.com

                            #14
                            Re: I need help with selecting from 2 identical tables in 2 separate databases

                            Bad news.. the query doesn't work after all.. turns out that the query
                            only produces data from Spring.Students and from data found in both
                            Spring.Students and Summer.Students ; all data unique to Summer.Students
                            is not copied over. :(

                            Phil

                            Bill Karwin wrote:[color=blue]
                            > phillip.s.powel l@gmail.com wrote:[color=green]
                            > > 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)[/color]
                            >
                            > Great work! I can never remember if a given RDBMS product & version can
                            > support cross-database joins.
                            >
                            > Regards,
                            > Bill K.[/color]

                            Comment

                            • Bill Karwin

                              #15
                              Re: I need help with selecting from 2 identical tables in 2 separatedatabas es

                              phillip.s.powel l@gmail.com wrote:[color=blue]
                              > Bad news.. the query doesn't work after all.. turns out that the query
                              > only produces data from Spring.Students and from data found in both
                              > Spring.Students and Summer.Students ; all data unique to Summer.Students
                              > is not copied over. :([/color]

                              It also treats John Smith and James Smith as the same person.

                              You're almost there.

                              First, get the folks who are in both spring and summer.

                              CREATE TABLE blah
                              SELECT DISTINCT s.*
                              FROM db1.table1 s INNER JOIN db2.table1 u
                              ON LOWER(s.first_n ame) = LOWER(u.first_n ame)
                              AND LOWER(s.last_na me) = LOWER(u.last_na me);

                              Next, get the folks who are only in spring.

                              INSERT INTO blah
                              SELECT DISTINCT s.*
                              FROM db1.table1 s LEFT OUTER JOIN db2.table1 u
                              ON LOWER(s.first_n ame) = LOWER(u.first_n ame)
                              AND LOWER(s.last_na me) = LOWER(u.last_na me);
                              WHERE u.first_name IS NULL;

                              Next, get the folks who are only in summer.

                              INSERT INTO blah
                              SELECT DISTINCT u.*
                              FROM db1.table1 s RIGHT OUTER JOIN db2.table1 u
                              ON LOWER(s.first_n ame) = LOWER(u.first_n ame)
                              AND LOWER(s.last_na me) = LOWER(u.last_na me);
                              WHERE s.first_name IS NULL;

                              Regards,
                              Bill K.

                              Comment

                              Working...