473,387 Members | 1,536 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How do I compare these 2 tables ?

290 100+
Hi,

I have two comparisons that I need to do and they are a bit
beyond my knowledge, would appreciate some help. :)

I have two tables that have an identical structure,
one is the incoming daily update, tableA which I want to
compare to the history tableB.

Both structures look like this:
id, title, desc, data1, data2, ... data18

(1)
The first comparison is to check for new rows. I think I can
do this by using the LEFT JOIN command

Is this how I do it ?

Expand|Select|Wrap|Line Numbers
  1. $query = "SELECT tableA.*, tableB.* FROM tableA LEFT JOIN tableB ON tableA.id = tableB.id"; 
  2.  
  3. $result = mysql_query($query) or die(mysql_error());
  4.  
(2)
My second comparison is to compare all the rows that have same ids
but have differences in the other fields i.e. not NEW records but CHANGED records.
Jan 11 '10 #1
3 4281
Atli
5,058 Expert 4TB
Hey.

Would I be right in guessing that the goal of these comparisons is to insert the data from the "input" table into the "history" table, adding new rows as new rows but updating old rows with the new data? (New and old rows being defined by whether their IDs preexist in the history table.)

If so, you don't have to do that manually. You can use the ON DUPLICATE KEY UPDATE clause of the INSERT statement to do this automatically.

For example, say I have a "input_table" and "storage_table" tables who both share the exact same structure:
Expand|Select|Wrap|Line Numbers
  1. (
  2.     `id` Serial Primary Key, 
  3.     `title` VarChar(255) Not Null Default 'Untitled', 
  4.     `desc` VarChar(255) Not Null Default 'No description'
  5. )
Given that they have the following data:
Expand|Select|Wrap|Line Numbers
  1. INSERT INTO `storage_table`
  2.     (`id`, `title`, `desc`)
  3. VALUES
  4.     (1, 'First in storage', 'This is the first row defined in the storage table'),
  5.     (2, 'Second in storage', 'This is the second row defined in the storage table'),
  6.     (3, 'Third in storage', 'This is the third row defined in the storage table');
  7.  
  8. INSERT INTO `input_table`
  9.     (`id`, `title`, `desc`)
  10. VALUES
  11.     (1, 'First from input', 'This is the first row, updated fromt he input table.'),
  12.     (3, 'Third from input', 'This is the third row, updated fromt he input table.'),
  13.     (4, 'Fourth from input', 'This is the fourth row, new from the input table.');
I could issue this command:
Expand|Select|Wrap|Line Numbers
  1. INSERT INTO `storage_table`
  2.     (`id`, `title`, `desc`)
  3. SELECT
  4.     `id`, `title`, `desc` 
  5.     FROM `input_table`
  6. ON DUPLICATE KEY UPDATE
  7.     `id`    = VALUES(`id`),
  8.     `title` = VALUES(`title`),
  9.     `desc`  = VALUES(`desc`);
After which the data in the "storage_table" would become:
Expand|Select|Wrap|Line Numbers
  1. +----+-------------------+------------------------------------------------------+
  2. | id | title             | desc                                                 |
  3. +----+-------------------+------------------------------------------------------+
  4. |  1 | First from input  | This is the first row, updated fromt he input table. | 
  5. |  2 | Second in storage | This is the second row defined in the storage table  | 
  6. |  3 | Third from input  | This is the third row, updated fromt he input table. | 
  7. |  4 | Fourth from input | This is the fourth row, new from the input table.    | 
  8. +----+-------------------+------------------------------------------------------+
See what I mean?
Jan 12 '10 #2
dgreenhouse
250 Expert 100+
UPDATE:
Looking at Atli's post I realized that (his?) recommendation works in one fell-swoop.
I'd go with that... But you will need to specify all the column names...
You can leave out the column names in insert and the sub-select, but you WILL need all of the column names after the 'on duplicate key update' clause.
i.e.
Expand|Select|Wrap|Line Numbers
  1. insert into tableb (select * from tablea)
  2. on duplicate key update
  3. id = values(id),
  4. title = values(title),
  5. `desc` = values(`desc`),
  6. data1 = values(data1),
  7. ...
  8. data18 = values(data18);
  9.  
  10. Note the backticks ` encapsulating `desc`
  11. desc is a reserved word in MySQL
  12. - ergo the need for the backtick.
  13. As Atli showed, it's probably best to always use backticks 
  14. to avoid query failures when using reserved words as 
  15. column and/or table names. 
  16. But it's best to avoid reserved words altogether.
  17.  
END-UPDATE:

For the first criteria - (pulling new records in) - this should work:
insert into tableB (select * from tableA where id not in (select id from tableB));

For the second criteria, you could use Atli's suggestion by using the "on duplicate key update" clause of an insert statement.

The one problem I see with this is when you get a lot of records in your history table (tableB). The other way requires a fairly complex select/insert statement.

But it's best to K.I.S.S.

Hope that helps...

By the way, unless there's a compelling reason to have the tables structured the way you have them, I can foresee problems later when you might want to expand the application.

(i.e. having that many fields named data1, data2, ..., data18)

But there's nothing inherently bad about the structure if that's indeed what you need.

(note: if indeed your structure is like data1,data2..., it will make the more complex select update command(s) easier.)

If you haven't already, look into "normalization", but as many times is the case, it's just too hard to go back and redesign an application that works for the most part. But DO look into normalization for future projects.
Jan 13 '10 #3
Atli
5,058 Expert 4TB
@dgreenhouse
I agree, it is very important to keep the normalization rules in mind when designing you databases. Normalized databases are generally easier to maintain and upgrade.

You can check out Database Normalization and Table Structures in our MS Access forums. It describes these rules very nicely.

@dgreenhouse
Indeed :)
Jan 13 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Maur | last post by:
Hi all, I have 2 tables say t_OLD and t_NEW. The new has corrections for audit purposes. They are identical in all respects (i.e. new is a copy of old and then changes are made to t_new) ...
5
by: Megan | last post by:
Hi everybody- I'm helping a friend with a music database. She has an old one and is creating a new one. She wants to compare records and fields in the old database with records and fields in the...
8
by: Vincent | last post by:
has any one seen a program to compare mdbs'. I have ran into a few of them, but none seem to really do that job. Basically what I need to do is, take 2 access mdb's and check the differences...
4
by: dfs9 | last post by:
In the article "Delete Duplicate Records From Access Tables" By Danny Lesandrini writes the following: This final suggestion is the most flexible and accurate. Given any table, it generates a...
9
by: VMI | last post by:
I have two tables and I want to compare these two tables with a query( ie. "select * from A where B.key = A.key") and the result will be stored in a 3rd table (table C). is this possible? If...
1
by: Mark | last post by:
by m.r.davies I have 2 tables on seperate Db's (and servers) I want to use a datareader on the first table to pick the booking ref, and then use that booking ref to query the 2nd DB when i have...
0
by: Shaw | last post by:
Our database is constantly updated (input data) from another DB, and sometimes it crashes our ASP.NET applications. My boss told me to write a DB utility app to check DB and make sure all apps are...
17
by: Mark A | last post by:
DB2 8.2 for Linux, FP 10 (also performs the same on DB2 8.2 for Windoes, FP 11). Using the SAMPLE database, tables EMP and EMLOYEE. In the followng stored procedure, 2 NULL columns (COMM) are...
5
by: Edd E | last post by:
Hi, I have a database to store my analyses (Access 2002, WinXp), the basic structure is: TABLE 1 = Sample Info TABLE 2 = Analysis type 1 TABLE 3 = Analysis type 2 TABLE 4 = Analysis type 3 ...
5
by: chazzy69 | last post by:
Ok heres what im trying to achive i am constantly updating the information in a table but know i what know if the data im replacing it with is new, i.e. whether or not i already had the data in the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.