# Select and insert in same function

**URL:** <https://discourse.nodered.org/t/select-and-insert-in-same-function/40832>\
**Category:** Dashboard\
**Created:** [11 February 2021 17:20 UTC](https://discourse.nodered.org/t/select-and-insert-in-same-function/40832 "2021-02-11T17:20:57Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![lucianv](https://avatars.discourse-cdn.com/v4/letter/l/cab0a1/32.png) [@lucianv](https://discourse.nodered.org/u/lucianv)\
**Post date:** [11 February 2021 17:20 UTC](https://discourse.nodered.org/t/select-and-insert-in-same-function/40832/1 "2021-02-11T17:20:57Z")

</div>

Hi all,  
I am struggling and not been able to find a solution for following problem:  
I get a message (from mqtt - not sure how relevant is) which can be 0 or 1 , and I want to check if is already in the table before insert it. Basically if last inserted value is1 and message is 1 then do nothing, else insert it. I am able to do operations separately but I am not able to combine these operations in one single function....  
Thank you in advance !

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [11 February 2021 23:02 UTC](https://discourse.nodered.org/t/select-and-insert-in-same-function/40832/2 "2021-02-11T23:02:59Z")

</div>

What 'table' are you talking about? Is it a database? a file? a ui\_table?

A little more information would be helpful

---

<div class="post-metadata">

**Author:** ![lucianv](https://avatars.discourse-cdn.com/v4/letter/l/cab0a1/32.png) [@lucianv](https://discourse.nodered.org/u/lucianv)\
**Post date:** [12 February 2021 07:09 UTC](https://discourse.nodered.org/t/select-and-insert-in-same-function/40832/3 "2021-02-12T07:09:17Z")

</div>

I have table with 3 rows (mysql), (id, status, time). Periodically i receive a status message which can be 0 or 1. I don t want to fill in the table with 1 (one after another) if i have 100 times status 1, i just want to do the status insert only if status received is different from last inserted value.

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [12 February 2021 08:15 UTC](https://discourse.nodered.org/t/select-and-insert-in-same-function/40832/4 "2021-02-12T08:15:32Z")

</div>

Could you use an RBE node before the insert to filter out all duplicate status ? Depends if you want to update or insert. I think mysql does also support upsert syntax if needed.

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [12 February 2021 12:14 UTC](https://discourse.nodered.org/t/select-and-insert-in-same-function/40832/5 "2021-02-12T12:14:54Z")

</div>

Mysql has an if statement so you should be able to do it in one query

> **[MySQL INSERT IF (custom if statements)](https://www.google.com/url?q=https:%2F%2Fstackoverflow.com%2Fquestions%2F6854996%2Fmysql-insert-if-custom-if-statements&sa=U&ved=2ahUKEwjk-tugqeTuAhWFwuYKHfHbB88QFjAAegQICxAB&usg=AOvVaw3ZiCEOnDKa5LxA83QEV9oW)**
>
> First, here's the concise summary of the question:
> 
> Is it possible to run an INSERT statement conditionally?
> Something akin to this:
> 
> IF(expression) INSERT...
> Now, I know I can do this with a stored

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [26 February 2021 12:15 UTC](https://discourse.nodered.org/t/select-and-insert-in-same-function/40832/6 "2021-02-26T12:15:24Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
