This is a read-only copy of the MariaDB Knowledgebase generated on 2024-11-17. For the latest, interactive version please visit https://mariadb.com/kb/.

Conditionally updating a field based on value changes

I'm currently working in a local mariadb 10.5.13

I have the following statement which updates PartsInventory and sets a last_modified = file_date

Problem is, I push inventory to another server if it has changed and last_modified indicates the last time we saw the sku, not if the data actually changed. Therefore, I added a second column store_modified and tried to make it a conditional insert (see below) BUT the date never updates

``` INSERT INTO PartsInventory (customer_id, make, part_no, part_desc, source_code, entry_date, last_sale, last_purchase, qoh, qavl, qpr, qps, qrobo, reorder_point, min_qty, max_qty, openro_qty, openro_cost, stock_status, bin1, bin2, 6mth_avg_sales, last_cost, avg_cost, last_pca_date, fm_list_price, base_list_price, base_cost_price, base_trade_price, int_pub_price, height, width, length, weight, movement_code, created, last_modified, store_modified) VALUES ( ?, ?, ?, ?, ?, -- 5 STR_TO_DATE(?, '%d/%m/%y'), -- 1 STR_TO_DATE(?, '%d/%m/%y'), -- 1 STR_TO_DATE(?, '%d/%m/%y'), -- 1 ?, ?, ?, ?, ?, ?, ?, ?, -- 8 ?, ?, ?, ?, ?, ?, ?, ?, -- 8 STR_TO_DATE(?, '%d/%m/%y'), -- 1 ?, ?, ?, ?, ?, ?, ?, ?, -- 8 ?, ?, ?, ?, ? ) -- 5 ON DUPLICATE KEY UPDATE part_desc = COALESCE( VALUES(part_desc), part_desc), source_code = COALESCE( VALUES(source_code), source_code), last_sale = COALESCE( VALUES(last_sale), last_sale), last_purchase = COALESCE( VALUES(last_purchase), last_purchase), qoh = COALESCE( VALUES(qoh), qoh), qavl = COALESCE( VALUES(qavl), qavl), qpr = COALESCE( VALUES(qpr), qpr), qps = COALESCE( VALUES(qps), qps), qrobo = COALESCE( VALUES(qrobo), qrobo), reorder_point = COALESCE( VALUES(reorder_point), reorder_point), min_qty = COALESCE( VALUES(min_qty), min_qty), max_qty = COALESCE( VALUES(max_qty), max_qty), openro_qty = COALESCE( VALUES(openro_qty), openro_qty), openro_cost = COALESCE( VALUES(openro_cost), openro_cost), stock_status = COALESCE( VALUES(stock_status), stock_status), bin1 = COALESCE( VALUES(bin1), bin1), bin2 = COALESCE( VALUES(bin2), bin2), 6mth_avg_sales = COALESCE( VALUES(6mth_avg_sales), 6mth_avg_sales), last_cost = COALESCE( VALUES(last_cost), last_cost), avg_cost = COALESCE( VALUES(avg_cost), avg_cost), last_pca_date = COALESCE( VALUES(last_pca_date), last_pca_date), fm_list_price = COALESCE( VALUES(fm_list_price), fm_list_price), base_list_price = COALESCE( VALUES(base_list_price), base_list_price), base_cost_price = COALESCE( VALUES(base_cost_price), base_cost_price), base_trade_price = COALESCE( VALUES(base_trade_price), base_trade_price), int_pub_price = COALESCE( VALUES(int_pub_price), int_pub_price), height = COALESCE( VALUES(height), height), width = COALESCE( VALUES(width), width), length = COALESCE( VALUES(length), length), weight = COALESCE( VALUES(weight), weight), movement_code = COALESCE( VALUES(movement_code), movement_code), last_modified = VALUES(last_modified), store_modified = IF( (qavl != VALUES(qavl) OR int_pub_price != VALUES(int_pub_price) OR height != VALUES(height) OR width != VALUES(width) OR length != VALUES(length) OR weight != VALUES(weight), VALUES(store_modified), store_modified ) ```

The file changes the qavl AND int_pub_price but the store_modified never changes.

Can I use IF statements in a ON DUPLICATE ... ?

Content reproduced on this site is the property of its respective owners, and this content is not reviewed in advance by MariaDB. The views, information and opinions expressed by this content do not necessarily represent those of MariaDB or any other party.