简体   繁体   中英

SQL update table and INSERT only new lines with selected fields from another table

I am trying to update mysql databases of 2 sites in order to have both the same products, I need to find a query that will add ONLY new product lines to the product table, I am trying with:

INSERT INTO pre_upde_prop 
(id
,pro_name
,pro_alias
,agent_id
,ref
,category_id
,price
,pro_small_desc
,pro_full_desc
,pro_type
,isFeatured
,published
,isSold
,note
,city
,state
,country
,province
,postcode
,hits
,created
,modified
,bed_room
,square_feet
,lot_size
,number_of_floors
,parking
,address
) 
SELECT id
     , name
     , alias
     , agent_id
     , ref
     , cid
     , price
     , description
     , text
     , type
     , featured
     , published
     , available
     , description
     , lid
     , sid
     , cyid
     , lid 
     , postcode
     , hits
     , listdate
     , refresh_time
     , bedrooms
     , area
     , covered_area
     , garage
     , garage
     , years 
  FROM pre2_properties_products;

but when i run this query it adds duplicates of all lines. I need to find a query that, for example, starts the insert at line id number 6000, for example, inserting only new line after 6000.

Both table have same product ids.

You need some way to restrict the records that are going into via the INSERT. Currently you are SELECTing from a table without any criteria. What you need is to restrict the results of that SELECT statement to eliminate any records that already exist.

Where it would be easy enough to add WHERE ID > 6000 to the end of the query that you've shown, this is of little use for anything more than an ad-hoc update. More stable methods are a) a frustrated join against the table, or b) a WHERE NOT IN/WHERE NOT EXISTS clause:

Method 0:

INSERT INTO pre_upde_prop (...) 
SELECT ... 
FROM pre2_properties_products
WHERE id > 6000;

Method 1:

INSERT INTO pre_upde_prop (...) 
SELECT ... 
FROM pre2_properties_products AS a
LEFT JOIN pre_udpe_prop AS b ON b.ID = a.ID
WHERE b.ID IS NULL

Method 2:

INSERT INTO pre_upde_prop (...) 
SELECT ... 
FROM pre2_properties_products AS a
WHERE id NOT EXISTS (
  SELECT id FROM pre_udpe_prop
);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM