The old mysql table is deduplicated and the new table is imported with a new value of the primary key id.

Source: Internet
Author: User

The old mysql table is deduplicated and the new table is imported with a new value of the primary key id.

Business Requirements:
Table A has the id, n1, n2, and n3 fields to create an empty table,

Table B has fields such as id, n1, n2, n3, n4, and n5, which are old tables containing data,

The records in n1, n2, and n3 combinations in Table B are de-duplicated and then imported into Table A. The primary key of Table A must also be entered.

Solution:Deduplicate the records in Table B after query, and orchestrate the sequence number for the query results. Use the sequence number as the id value of the Table A to be inserted. The SQL statement is as follows:

Insert into a table (id, n1, n2, n3) select @ row: = @ row + 1 AS id, t1. * from (select distinct n1, n2, n3 from B table) t1, (select @ row: = 0) as t2

Related Article

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.