Node Red if else functionality

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

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

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

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

php - How can I do 'insert if not exists' in MySQL? - Stack Overflow

This did the trick, thanks!

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