473,549 Members | 2,731 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to compare entries in 2 seperate tables and store results in a 3rd table

196 New Member
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 table.

So my method for this is to first copy the information to be updated into another table (the 2nd table), then allow the information in the first table to updated (via deleting the current information and re-entering it).

Then i what to compare the two tables to see if there is any new rows that where not there before updating the data in the first tables. (that is i want to compare both table 1 and 2 at this point).

And this is where im struggling to find a method for comparing the two tables, to identify any new rows.

So i was wondering if anyone had any ideas on how to compare these two seperate tables.

Note: These tables are all in the same MYSQL database,

Any help would be greatly appreciated
May 24 '09 #1
5 3036
Atli
5,058 Recognized Expert Expert
I don't get it.

You have table A, which has a bunch of rows.
Then you get table B, which might have all the old rows as well as new once.

And you need to figure out which rows in table B are new compared to the rows in table A.

Is that right, or am I not getting it?

If not, please explain this a bit better, preferably with examples.
May 24 '09 #2
chazzy69
196 New Member
Ok to start with i have a single table in which all information is constantly being updated, currently i no way to be able to tell when im adding new rows to a table or just the old ones, my databases would be to big if i just tried to store all data.

So what i need to do now is to determine when i add a new row to a table but all the old rows are deleted before the new rows are added, this is to save space as i explained above.

Therefore my method for getting around this was to make a backup of the rows i was deleting and then comparing it against the new rows that i add, so in this way it tell if there are any rows that i have just added that are new that is they where not there before i deleted the old data.

So i have figured out how to transfer the data to another table before i delete the old data. My current problem is being able to determine the difference in the old and new data between the tables that is is there a row contained in the orignal table that was not there before i updated the rows.

I did find a bit of code -

Expand|Select|Wrap|Line Numbers
  1. SELECT t1.id FROM t1 LEFT JOIN t2 ON t1.id=t2.id WHERE t2.id IS NULL
But when i implemented it it seemed the where clauses are not working properly cause i limited it to compare 30 rows form each and see if there are any that are different but i found that it got 933 differences instead of 3 differences that actually exist.

Anyway i dont have to use that code but any alternate methods would be great,

Hope that cleared it up a bit :)
May 24 '09 #3
Atli
5,058 Recognized Expert Expert
Ok.

So you are essentially deleting the old table and rebuilding it with new data, most of which will be the same as the old data.

This is how I am picturing this...
Expand|Select|Wrap|Line Numbers
  1. CREATE TABLE t1Backup SELECT * FROM t1;
  2. TRUNCATE t1;
  3.  
  4. INSERT INTO t1 VALUES /* All your new data */;
  5.  
  6. SELECT t1.id
  7. FROM t1 LEFT JOIN t1Backup
  8.   ON t1.id = t1Backup.id
  9. WHERE t1Backup.id IS NULL;
  10.  
  11. DROP TABLE t1Backup;
Which would give you the IDs of all the new rows.

Note that if you don't TRUNCATE or DROP the main table before recreating it the SELECT statement will return all the rows, as the IDs will not be reset.
May 24 '09 #4
chazzy69
196 New Member
Ok this sounds good but i was wondering becuase im only deleting part of my table and updating at a time perhaps i could limit or restrict what the function uses to compare with for example in the code u used above

Expand|Select|Wrap|Line Numbers
  1. WHERE t1Backup.id IS NULL
;

could i change it to say perhaps-

Expand|Select|Wrap|Line Numbers
  1. WHERE t1Backup.id1 IS NULL AND t1backup.id2 = "to some value" AND t1backup.id3 = "to some other value"
;

??

Thanks for the help btw :)
May 24 '09 #5
chazzy69
196 New Member
Ok don't worry about my last post i have figured out how to get it working.

Just wondering if when you see the results from the comparison is it possible to show all the colums of the row that was different because at the moment all it outputs is the different field i used to compare with.

i.e. if im comparing - t1.id1 to t2.id1

it will output only .id1 rather than the entire row like .id1 | .id2 | .id3

Hope that makes sense,

Also is it possible to store results from a query into a table??

Thanks again for all the help
May 25 '09 #6

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

Similar topics

5
10857
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 new database. For instance, her old database has a table with Band Info in it. Her new database also has a table with Band Info in it but slightly...
8
7122
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 between the 2. i am talking about tables, forms, queries, the whole ball of wax. Most of the programs jus do tables, that is the easy part. Also...
9
6603
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 necessary, I can create the schema for the 3rd table (C) since it'll be exactly like table A. But I'm more interested in being able to store the resulting...
1
1876
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 the correct row in the 2nd DB I want to compare each field in the 2 tables to see if they match Every time i try and think how i end up with a while...
0
1183
by: Frank | last post by:
Hello, Developing an app where the user fills out a sometimes quite lengthy form of chkboxes, txtboxes, radbtns, etc. User responses are saved to a mySql db, which the user can later edit. When the user chooses to edit, I pull the responses from the db, toss them in a dataset, check the checks, fill the txtboxes, etc.,etc. The user then...
4
6657
by: Lamis | last post by:
Hi, what is the best way to compare 2 haschtables contatining objects. the objects has 2 property, name & value. I need to print out the differences -- LZ
5
2654
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 ....where each table stores results of a different type of analysis
0
3095
by: TechnoAtif | last post by:
<?php include "dbconnect.php"; include "commonFunc.php"; ?> <!----------------------------------> <table width="80%" border="1" cellpadding="2" cellspacing="0"> <tr > <td colspan="2"><p>
1
2542
by: cssExp | last post by:
Hi, Assume i have two tables. "accounts" and "accounts_logs" "accounts" (accid, type) "accounts_log" (accid, ip, timestamp) Here i want to choose a radom 3 accids from "accounts" if available, but each of them should be compared with "accounts_log". Lests say a one of the accid chose is A1, then take users IP and current time -...
0
7518
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7715
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. ...
0
7956
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7469
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...
0
7808
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...
0
5087
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...
0
3480
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1935
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1057
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.