Emergency!! How to compare the contents of two tables in a database, delete the same data, and keep different data

Source: Internet
Author: User
Php

URGENT Request!!!

Excuse me, how to compare the data in two tables. If the data is the same, check one article and delete it, if the data is different, keep it separately.

Finally, the different data from the two tables is exported and imported into another table dedicated to the error.


Reply to discussion (solution)

I'd better take a look at the code, dear.

is the data the same? is judged by a field, or if each field in the datasheet is compared
You can query out all the data in both tables, and cycle through the comparison

NN, two tables in the columns are the same, but how to write, multi-column comparison, and then compare, the same deleted, not the same in the respective table, please, there is no detailed code for reference

Oh, the simplest way is to query a table all the data loop comparison with another table

There is also a direct linked table query,
For example select A,id,b.id from a, where a.filed1=b.filed1 and A.filed2=b.filed2 and A.FILED3=B.FILED3

This query then deletes the

But it's a little slow to ask for a high-performance rate.

1. Obtain the record number of table a standby select COUNT (*) as CNT from A
2. Extract the records of table B different from table a select * FROM A Union SELECT * FROM B limit CNT
3. Get the number of records in table B spare select COUNT (*) as CNT from B
4. Extract the records of table A different from table B select * FROM B Union SELECT * FROM A limit CNT

What do you do back there? You decide for yourself.

You can consider using stored procedures

Don't be so troublesome, send out the watch.
CREATE TABLE.
INSERT INTO..

And make a template out of the results you want.

The key now is how to delete the same data from the two tables and keep the different data?

Pro, give advice, best have code, thank you

You should give the test data.

For example:


The two tables are the same, now delete the same data and keep the different data

Select A,id,b.id from a b where a.filed1=b.filed1 and A.filed2=b.filed2 and A.FILED3=B.FILED3
And then delete it.

Dear friends, Thank you for your support, with the help of everyone has been resolved

Delete A, a from a left JOIN B on A.c_num=b.c_num and A.c_jifen=b.c_jifen where a.c_num=b
. c_num

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.