Copy data from one table to another with specific condition

user3855851

I want to copy data from one table to another but only which has processed='1' in the column value after a specific date.

I have code which could do it but its taking a long time to execute.

"INSERT INTO eamglo5_billingsystem.`consignment` ( 
`consignment_status`,
  `account`,
  `awb`,
  `hawb`,
  `service`,
  `handling`,
  `reference`,
  `date_submitted`,
  `date_imported`,
  `date_printed`,
  `printed_file_id`,
  `date_received`,
  `date_booked`,
  `booked_file_id`,
  `date_exported`,
  `export_file_id`,
  `company`,
  `contact`,
  `address_line_1`,
  `address_line_2`,
  `address_line_3`,
  `id`
)
     SELECT
        'Y',
       `account`,
      `awb`,
      `hawb`,
      `service`,
      `handling`,
      `reference`,
      `date_submitted`,
      `date_imported`,
      `date_printed`,
      `printed_file_id`,
      `date_received`,
      `date_booked`,
      `booked_file_id`,
      `date_exported`,
      `export_file_id`,
      `company`,
      `contact`,
      `address_line_1`,
      `address_line_2`,
      `address_line_3`,
      `id` 
      FROM  `eamglo5_singaporelive`.`consignment` 
      left join  (
        SELECT eamglo5_billingsystem.`consignment`.`id` as id1 
         FROM eamglo5_billingsystem.`consignment` 
      ) t  ON  `eamglo5_singaporelive`.`consignment`.id >id1
      WHERE `eamglo5_singaporelive`.`consignment`.`processed`=1 
       and `eamglo5_singaporelive`.`consignment`.date_booked>'2018-07-17'

Expected: Should copy data from eamglo5_singaporelive.consignment table into eamglo5_billingsystem.consignment table with only processed=1 values.

Actual: Taking an infinite time to execute and fetch the rows.

Paul Spiegel

Your LEFT JOIN with the condition consignment.id >id1 is almost creating a catesian product. What you probably want, is to insert only rows with a higher id from the source table than the highest id1 in the destination table. You should use a SELECT MAX(id) subquery instead:

SELECT [..]
FROM  `eamglo5_singaporelive`.`consignment` 
WHERE `eamglo5_singaporelive`.`consignment`.`processed`=1 
  and `eamglo5_singaporelive`.`consignment`.date_booked>'2018-07-17'
  and `eamglo5_singaporelive`.`consignment`.id > (
     SELECT MAX(id1) FROM eamglo5_billingsystem.`consignment`
  )

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

copy table data from one db to another with where condition

Copy Data from one table to another table

Copy Data from one hbase table to another

Copy table data from one database to another

Copy from one temp table to another temp table on condition base?

Can I copy data from one table to another in SQLite using a join condition?

Copy data from one cell to another if condition met VBA

Excel copy data from one sheet to another based on a condition

Copy Row Data from one to another sheet base on Yes condition

Copy only specific rows from one table to another in PHP and MySQL

How to copy data from one table to another table in Firebase?

Copy and then insert data from one cell in a table to another cell in that table

MYSQL, copy data from one table to another table with increment in varchar

SQL - copy data from one table coloum to another table coloum

SQL - copy data from one table column to another table column

How to copy data from one table to another new table in MySQL?

How do i copy data from one table to another table?

How to copy data from one table to another table based on criteria

Copy data from one sheet to another spreadsheet exclude specific columns

How to copy specific columns of data from one sheet to another

SQL migrate data from one table to another on condition

Insert data from a table to another one in MySQL with where condition

insert data from one table into another using a condition mysql

How copy data from one table into another? PHP

mysql copy data from one table to another using foreign key

MySQL copy data from one table to another in different fields

SQL Server Copy Random data from one table to another

How to copy data into empty table from another one by UPDATE?

Copy data from one table to another - Ignore duplicates Postgresql