Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Compare two tables data

 
Abhinav Rohatgi
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello eveyone,
There are 2 tables both tables having the same columns(id, name,salary). The salary of Table 2 is changed for some id's. Now I want the data for those whose salary is different from Table 1.

I tried this but not success. Please someone help me.

 
Paul Clapham
Sheriff
Posts: 21579
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wouldn't it be easier to just join the two tables and use a where-clause to only include the matched records which have different salary values? As a general rule you should always prefer having the database engine do that sort of logic rather than writing Java code to do it.
 
Paul Clapham
Sheriff
Posts: 21579
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch!

(By the way are the salary values really stored as String data in your database tables? Hopefully they aren't. And then you shouldn't treat them as String in your Java code either.)
 
chris webster
Bartender
Posts: 2407
33
Linux Oracle Postgres Database Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do this in the database with SQL, not in Java. Depending which database you're on, you may be able to use set operators to make it easier. Using SQL to run the query in the database means you don't have to move all the data across the network, you can use a proper query language, and the query engine can take advantage of any indexes to improve performance.
 
Paul Clapham
Sheriff
Posts: 21579
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And also by the way, don't use the == to check whether the contents of two String objects are the same. You should use the equals() method instead.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic