-1

I would like to replace a value if the date/time for a row in table is the same for the one he try to insert. If the time/date doesn't exist in the table then insert it.

If you se the picture. The value nr 2 to the end has the same time as the first value. Then it should replace the first one because they have the same time. Not get added last to the list. If the time doesn't exist, then it can be added.

I have tried with REPLACE and ON DUPLICATE KEY UPDATE, but I cant get it to work...

"INSERT INTO flowAvarageValuesDays (time, value) VALUES (%s, %s) ON DUPLICATE KEY UPDATE avarageFlowValueDays=%s", (time,value,value)

and

"REPLACE INTO flowAvarageValuesDays (time, value) VALUES (%s, %s)",
            (time, value)

Table

5
  • I cant get it to work... is not very helpful. Post code minimal reproducible example and the full traceback you get Commented Mar 10, 2020 at 18:17
  • Is the time column a key? It won't work unless it's a key. Commented Mar 10, 2020 at 18:23
  • How can I set the tiem column as a key ? Commented Mar 10, 2020 at 18:25
  • @PederWard: look at this: stackoverflow.com/a/19950545/42346 Commented Mar 10, 2020 at 18:27
  • You can add condition for your query if(time = (SELECT time FROM table WHERE %s IN (time),INSERT,UPDATE) If time doesn't exist then it will insert else update Commented Mar 11, 2020 at 0:16

1 Answer 1

1
IF(SELECT time FROM table WHERE %s IN(time), INSERT INTO flowAvarageValuesDays (time, value) VALUES (%s, %s) ON DUPLICATE KEY UPDATE avarageFlowValueDays=%s, (time,value),REPLACE INTO flowAvarageValuesDays (time, value) VALUES (%s, %s),

(time, value))

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

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.