Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9100853
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T00:58:17+00:00 2026-06-17T00:58:17+00:00

Been looking around the web for a while now and don’t seem to be

  • 0

Been looking around the web for a while now and don’t seem to be able to come across anything similar to what I want. I know it’s something to do with the way I’m writing my query but any help would be appreciated.

The basics of what I am trying to do is:

  • Insert some items into a table if it does not exist
  • Update an item if it does exist

It exists in the format:

name, barcode, item, quantity, location, price and date

name – can be used in several rows
barcode – is for a specific item but can be used as several locations
item – is the same as barcode but contains the name
quantity – self explanatory
location – this can be different locations
price – that is attached to a specific item
date – last time that item was purchased

The tricky thing is, a “name” can have several items (barcode and item) at different locations for different prices. The idea is that a customer can see how much they bought an item for at a set time, so they know how much they would need to sell it for.

However the price that they bought it at can vary so they need to create another row in the table if the price is different from a previous purchase.

The idea behind the whole thing is for it to record how much a “name” has of each item at each location and then the price they bought it at and when they last purchased it.

Hope that makes sense.

In psuedo code:

    Insert into table if does not exist
    - name, barcode, item, quantity, location, price and date
    If name, barcode, item, location and price are the same
    - Update quantity and date (if more recent)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T00:58:19+00:00Added an answer on June 17, 2026 at 12:58 am

    First, add a UNIQUE constraint on name, barcode, item, location and price.

    ALTER TABLE  tableX
      ADD CONSTRAINT tableX_UQ
        UNIQUE (name, barcode, item, location, price) ;
    

    Then you can use INSERT INTO ... ON DUPLICATE KEY UPDATE:

    INSERT INTO tableX
      (name, barcode, item, location, price, quantity, date)
    VALUES
      (?, ?, ?, ?, ?, ?, ?)
    ON DUPLICATE KEY UPDATE
      quantity = CASE WHEN VALUES(date) > date
                   THEN quantity + VALUES(quantity)        -- add quantity
                   ELSE quantity                           -- or leave as it is
                 END
    , date = CASE WHEN VALUES(date) > date
                   THEN VALUES(date) ;                     -- set date to new date
                   ELSE date                               -- or leave as it is
                 END 
    

    REPLACE could also be used but there are differences in the behaviour (which especially matter if you have foreign keys). For details, see this question “INSERT IGNORE” vs “INSERT … ON DUPLICATE KEY UPDATE” and the answer by @Bill Kawin which discusses the differences between INSERT IGNORE, INSERT ... ON DUPLICATE KEY and REPLACE.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Been looking around the web, but not found anything so far... can anyone help?
If been looking around the web and can't seem to find any good solutions
So I've been looking around the web for a Python Speech recognition, and I
I have been looking around the web for this but cannot really find a
I have been looking around on the web and found some articles about the
Been looking around a bit and I can't seem to find any help on
I've been looking around for a solution for several hours now, and maybe I'm
I've been looking around on the web and I can't find a solution to
I have been using WCF web services for around about a year now and
I've been looking around the web and all the examples I've seen have a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.