Welcome to dbFreaks.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

Comparing 2 tables

 
   Database Help (Home) -> Java RSS
Next:  MySql - creating a schema.  
Author Message
m

External


Since: Feb 19, 2008
Posts: 1



(Msg. 1) Posted: Thu Mar 27, 2008 10:00 pm
Post subject: Comparing 2 tables
Archived from groups: comp>lang>java>databases (more info?)

Hi All

I need to compare 2 tables ( I need to eliminate 2 columns in the
table while comparing, they are dates and userids which wil be
different. )

Example:

TableA( colA, colB, colC, colD, colE)
TableB( colA, colB, colC, colD, colE)

I need to compare TableA and TableB , for me they are equal if
contents of colA, colB, colC are same , need not compare colD and
colE.

I write jdbc program which reads tables and compare row by row, just I
think it is laborious. Is there a better way of doing this. Please let
me know.

Thanks

 >> Stay informed about: Comparing 2 tables 
Back to top
Login to vote
David Harper

External


Since: Jan 25, 2004
Posts: 32



(Msg. 2) Posted: Fri Mar 28, 2008 7:05 am
Post subject: Re: Comparing 2 tables [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

m wrote:
> Hi All
>
> I need to compare 2 tables ( I need to eliminate 2 columns in the
> table while comparing, they are dates and userids which wil be
> different. )
>
> Example:
>
> TableA( colA, colB, colC, colD, colE)
> TableB( colA, colB, colC, colD, colE)
>
> I need to compare TableA and TableB , for me they are equal if
> contents of colA, colB, colC are same , need not compare colD and
> colE.
>
> I write jdbc program which reads tables and compare row by row, just I
> think it is laborious. Is there a better way of doing this. Please let
> me know.

You may want to consider a left join between the two tables, along the
lines of

select TableA.*,TableB.* from TableA left join TableB on
(TableA.colA = TableB.colA and TableA.colB = TableB.colB and
TableA.colC = TableB.colC)
where TableB.colD is null or TableB.colE is null;

This will show you which rows in TableA are not equal (by your criteria)
to rows in TableB.

You probably need to repeat the query exchanging TableA and TableB to
find rows in TableB which are not equal to rows in TableA.

This assumes, of course, that the triples (colA,colB,colC) are unique in
each table i.e. there is only one row in TableA with any given
combination of (colA,colB,colC) and likewise with TableB.

David Harper
Cambridge, England

 >> Stay informed about: Comparing 2 tables 
Back to top
Login to vote
Roedy Green

External


Since: Feb 12, 2008
Posts: 13



(Msg. 3) Posted: Fri Mar 28, 2008 12:33 pm
Post subject: Re: Comparing 2 tables [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On Thu, 27 Mar 2008 22:00:32 -0700 (PDT), m wrote,
quoted or indirectly quoted someone who said :

>I write jdbc program which reads tables and compare row by row, just I
>think it is laborious. Is there a better way of doing this. Please let
>me know.

do it in SQL with a where clause.. Presumably you want only that
match or differ.

This will probably be faster because no records have to be transported
to your application which may be running on a different box.
--

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
 >> Stay informed about: Comparing 2 tables 
Back to top
Login to vote
Lew

External


Since: Feb 15, 2008
Posts: 43



(Msg. 4) Posted: Fri Mar 28, 2008 10:23 pm
Post subject: Re: Comparing 2 tables [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Roedy Green wrote:
> On Thu, 27 Mar 2008 22:00:32 -0700 (PDT), m wrote,
> quoted or indirectly quoted someone who said :
>
>> I write jdbc program which reads tables and compare row by row, just I
>> think it is laborious. Is there a better way of doing this. Please let
>> me know.
>
> do it in SQL with a where clause.. Presumably you want only that
> match or differ.
>
> This will probably be faster because no records have to be transported
> to your application which may be running on a different box.

Given tables A and B that share a primary key (PK):
CREATE TABLE A ( id INTEGER PRIMARY KEY, etc. )
CREATE TABLE B ( id INTEGER PRIMARY KEY, etc. )

SELECT COUNT(*) FROM A WHERE id NOT IN (SELECT id FROM B);
and vice versa.

--
Lew
 >> Stay informed about: Comparing 2 tables 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
Comparing database dates: SELECT FROM WHERE Date(today) &l.. - I want to get in a java prgm all those database records whose MYDATE column contains a date greater equal a given other date (say: today). How do I specify this in a SQL query from Java? Assume the comparison date in java is in a java.util.Date field.....

SQLServer 2000 Driver for JDBC behaviour on SQL Server res.. - 1) My java code properly connects to the SQL Server DB via the SQL Server 2000 JDBC driver. 2) During one of our stress tests, we restarted the SQL Server to see how the code handles it. 3) My code doesn't really recognize that the SQL server is up and m...

Displaying contents from database in an applet - I have created a connection to an MSAccess database, but I am unable to display the results in an webbrowser. My question is therefore how do I retrieve and displaty the records of my database on a webpage? Kind regards Johan Reimers

Hibernate and log4j config under Tomcat. - "Under Tomcat 3.x and 4.x, you should place the log4j.properties under the WEB-INF/classes directory of your web-applications. Log4j will find the properties file and initialize itself. This is easy to do and it works." "The XML configu...

Java & MySQL show table status command - Hi, I am using java to retrieve a resultset with "show table status" mysql command. I am having problem reading the [name] and [engine] column. In the metadata object it is showing as VARCHAR but when I do getString() it is returning me the o...
   Database Help (Home) -> Java All times are: Pacific Time (US & Canada)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]