# Node Red if else functionality

**URL:** https://discourse.nodered.org/t/node-red-if-else-functionality/55386
**Category:** General
**Created:** [18 December 2021 13:02 UTC](https://discourse.nodered.org/t/node-red-if-else-functionality/55386 "2021-12-18T13:02:52Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Sebas\_t](https://avatars.discourse-cdn.com/v4/letter/s/ac8455/32.png) [@Sebas\_t](https://discourse.nodered.org/u/Sebas_t)
#### Post date: [18 December 2021 13:02 UTC](https://discourse.nodered.org/t/node-red-if-else-functionality/55386/1 "2021-12-18T13:02:52Z")

</div>

Hi,

I'm building an IoT MQTT platform with node red and a mysql database.  
My MQTT connection is working fine as well as the database connections.

However I need to store a connected device ID in my device tabel.  
Every time I receive data the device ID is send along with it so I extract the device ID from my msg.payload.

Now I first want to check if the device ID already exists in my tabel with a query.  
Currently I'm doing this whith the following query:

var strQuery = "SELECT COUNT(sensorADDR) FROM co2sensor WHERE sensorADDR = "+serverADDR+";";

When the device ID doesn't exist in the tabel I would like to add it to my device tabel using.

var strQuery2 = "INSERT INTO co2sensor (sensorADDR) VALUES ('"+serverADDR+"');";

So in essence I want to do the following.

if( COUNT(deviceID) == 0)  
{  
var strQuery2 = "INSERT INTO co2sensor (sensorADDR) VALUES ('"+serverADDR+"');";  
}  
else  
{  
Other code  
}

I've tried the following

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/7/17559d5415cc5ee53a40cd91d7b06c89284e0c68.png)

But I lose my original data from the test mqtt out.

Can anyone help me with this problem?  
With kind regards  
Sébastien

---

<div class="post-metadata">

### Author: ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)
#### Post date: [18 December 2021 13:15 UTC](https://discourse.nodered.org/t/node-red-if-else-functionality/55386/2 "2021-12-18T13:15:31Z")

</div>

Can you get rid of the select query by using INSERT INTO table ... ON DUPLICATE KEY UPDATE ... ?

---

<div class="post-metadata">

### Author: ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)
#### Post date: [18 December 2021 14:18 UTC](https://discourse.nodered.org/t/node-red-if-else-functionality/55386/3 "2021-12-18T14:18:30Z")

</div>

[php - How can I do 'insert if not exists' in MySQL? - Stack Overflow](https://stackoverflow.com/questions/1361340/how-can-i-do-insert-if-not-exists-in-mysql)

---

<div class="post-metadata">

### Author: ![Sebas\_t](https://avatars.discourse-cdn.com/v4/letter/s/ac8455/32.png) [@Sebas\_t](https://discourse.nodered.org/u/Sebas_t)
#### Post date: [18 December 2021 14:18 UTC](https://discourse.nodered.org/t/node-red-if-else-functionality/55386/4 "2021-12-18T14:18:59Z")

</div>

This did the trick, thanks!

---

<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: [16 February 2022 14:19 UTC](https://discourse.nodered.org/t/node-red-if-else-functionality/55386/5 "2022-02-16T14:19:39Z")

</div>

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