1

My table is made up of 3 values-

  • long
  • short
  • id

id is an auto increment int value and the rest are text.

My PHP script retrieves the latest version of a program every half an hour and stores it as long in an sql table. What I want is for the php script to check if the value is already in the table(i.e. there are no new versions). If it isn't I want it to insert the new data. Just to make this clear I am not replacing the value "long". I am merely making a new value of it and 'id' is keeping a record of how many versions there have been.

2 Answers 2

4

Use the EXISTS Keyword.

INSERT INTO Table (Col1, Col2, Col3)
Values(?, ?, ?)
WHERE NOT EXISTS (
SELECT Col1 FROM TABLE
WHERE Col1 = ? AND Col2 = ? AND Col3 =?)

You can work out which columns you actually need to test.

Sign up to request clarification or add additional context in comments.

Comments

0

You can put a UNIQUE index on the long column and use INSERT IGNORE:

INSERT IGNORE INTO versions (long, short)
VALUES (?, ?)

If the key already exists MySQL will generate a warning (not an error) and will neither insert nor update any rows.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.