# How to split data from node-red in mysql

**URL:** https://discourse.nodered.org/t/how-to-split-data-from-node-red-in-mysql/38299
**Category:** General
**Created:** [30 December 2020 20:01 UTC](https://discourse.nodered.org/t/how-to-split-data-from-node-red-in-mysql/38299 "2020-12-30T20:01:08Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![farahfijud](https://avatars.discourse-cdn.com/v4/letter/f/f04885/32.png) [@farahfijud](https://discourse.nodered.org/u/farahfijud)
#### Post date: [30 December 2020 20:01 UTC](https://discourse.nodered.org/t/how-to-split-data-from-node-red-in-mysql/38299/1 "2020-12-30T20:01:08Z")

</div>

I have 2 input from mqtt and i want to split the data/value into 2 column in mysql. For example i want to separate the value of temperature as 29.70 and humidity as 84.00. But based on my result the 2 column shown the same data/value.

![Capture1](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/3/333202d5fa4386d6511211a03be0d968312fc16a.jpeg)

 ![Capture](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/d/ed364f0dd36a23a66a8408da3bd3d913a187f284.jpeg)

This is my code at function node:

Temperature = msg.payload  
Humidity = msg.payload  
msg.topic = "INSERT INTO dht11 (Temperature,Humidity)"+" VALUES ('"+ Temperature +"','"+ Humidity +"')";  
return msg;

---

<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: [30 December 2020 20:33 UTC](https://discourse.nodered.org/t/how-to-split-data-from-node-red-in-mysql/38299/2 "2020-12-30T20:33:17Z")

</div>

You are feeding two separate inputs to the same function. In the function you are always putting the msg.payload into both fields so of course they will be the same.

In your listing, the first of every 2 entries is the temperature and the second is the humidity. In other words you have split by row not column.

This isn't easy to do with SQL which is why many of us use InfluxDB. As a timeseries db, it is much easier to manage individual entries.

If you really want to have 1 row for a pair of temp/hum values, you will need to use a context variable and work out when you have a pair of new values and only then write a row to the db.

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [30 December 2020 20:41 UTC](https://discourse.nodered.org/t/how-to-split-data-from-node-red-in-mysql/38299/3 "2020-12-30T20:41:40Z")

</div>

> [@TotallyInformation](#):
>
> If you really want to have 1 row for a pair of temp/hum values, you will need to use a context variable and work out when you have a pair of new values and only then write a row to the db.

Alternatively you could use a Join node to merge the two together following [this example from the cookbook](https://cookbook.nodered.org/basic/join-streams).

Yet another alternative may be appropriate if the temperature and humidity come from the same source. If they do then you could put them both into one object and pass that via MQTT, so they are both available ready for adding to the database.

However, unless you have a good reason to stick with mysql, and if the data are all time series, then use Influxdb instead, as already suggested.

---

<div class="post-metadata">

### Author: ![farahfijud](https://avatars.discourse-cdn.com/v4/letter/f/f04885/32.png) [@farahfijud](https://discourse.nodered.org/u/farahfijud)
#### Post date: [30 December 2020 20:55 UTC](https://discourse.nodered.org/t/how-to-split-data-from-node-red-in-mysql/38299/4 "2020-12-30T20:55:46Z")

</div>

Noted, thankyou for your help and explanation

---

<div class="post-metadata">

### Author: ![farahfijud](https://avatars.discourse-cdn.com/v4/letter/f/f04885/32.png) [@farahfijud](https://discourse.nodered.org/u/farahfijud)
#### Post date: [30 December 2020 20:58 UTC](https://discourse.nodered.org/t/how-to-split-data-from-node-red-in-mysql/38299/6 "2020-12-30T20:58:12Z")

</div>

Noted. ill try. Thankyou for your help.

---

<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: [28 February 2021 20:58 UTC](https://discourse.nodered.org/t/how-to-split-data-from-node-red-in-mysql/38299/7 "2021-02-28T20:58:17Z")

</div>

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