# Can't Differentiate data from different topics while inserting data into my sql database

**URL:** <https://discourse.nodered.org/t/cant-differentiate-data-from-different-topics-while-inserting-data-into-my-sql-database/48275>\
**Category:** General\
**Created:** [11 July 2021 16:32 UTC](https://discourse.nodered.org/t/cant-differentiate-data-from-different-topics-while-inserting-data-into-my-sql-database/48275 "2021-07-11T16:32:58Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![SubhanShamsi](https://avatars.discourse-cdn.com/v4/letter/s/6bbea6/32.png) [@SubhanShamsi](https://discourse.nodered.org/u/SubhanShamsi)\
**Post date:** [11 July 2021 16:32 UTC](https://discourse.nodered.org/t/cant-differentiate-data-from-different-topics-while-inserting-data-into-my-sql-database/48275/1 "2021-07-11T16:32:58Z")

</div>

msg.topic = "insert into metertable values ('','','"+msg.payload+"','"+msg.payload+"');"  
return msg;

 ![err1](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/b/8b96cd161ffd7f482164d5087cd23ee30cdafde1.jpeg)

While the data is different and its insert the same values on both column in database.

 ![err2](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/b/6b97c65744087f8bcc3e6019d60bf8d04fb4421a.jpeg)

I also tried to create variable and save coming data but its showed undefined.

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [11 July 2021 16:35 UTC](https://discourse.nodered.org/t/cant-differentiate-data-from-different-topics-while-inserting-data-into-my-sql-database/48275/2 "2021-07-11T16:35:38Z")

</div>

There is a recipe in the cookbook that describes how to combine different streams of messages into a single message - [Create a single message from separate streams of messages : Node-RED](https://cookbook.nodered.org/basic/join-streams)

---

<div class="post-metadata">

**Author:** ![SubhanShamsi](https://avatars.discourse-cdn.com/v4/letter/s/6bbea6/32.png) [@SubhanShamsi](https://discourse.nodered.org/u/SubhanShamsi)\
**Post date:** [11 July 2021 16:46 UTC](https://discourse.nodered.org/t/cant-differentiate-data-from-different-topics-while-inserting-data-into-my-sql-database/48275/3 "2021-07-11T16:46:26Z")

</div>

But afer joining how can i insert these values separatly with their repective column into mysql database ??

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [11 July 2021 16:50 UTC](https://discourse.nodered.org/t/cant-differentiate-data-from-different-topics-while-inserting-data-into-my-sql-database/48275/4 "2021-07-11T16:50:05Z")

</div>

I suggest you take it one step at a time.

Get the messages joined into one. Pass that to a debug node and confirm you have all the data from both messages you need.

You can then construct a query that does the insert taking the values from the message.

---

<div class="post-metadata">

**Author:** ![SubhanShamsi](https://avatars.discourse-cdn.com/v4/letter/s/6bbea6/32.png) [@SubhanShamsi](https://discourse.nodered.org/u/SubhanShamsi)\
**Post date:** [11 July 2021 17:09 UTC](https://discourse.nodered.org/t/cant-differentiate-data-from-different-topics-while-inserting-data-into-my-sql-database/48275/5 "2021-07-11T17:09:53Z")

</div>

1 step was done and its working.  
Then how i can insert into database ??

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [11 July 2021 17:47 UTC](https://discourse.nodered.org/t/cant-differentiate-data-from-different-topics-while-inserting-data-into-my-sql-database/48275/6 "2021-07-11T17:47:30Z")

</div>

What exactly does the combined message now look like?  
How have you configured the SQL node?

---

<div class="post-metadata">

**Author:** ![SubhanShamsi](https://avatars.discourse-cdn.com/v4/letter/s/6bbea6/32.png) [@SubhanShamsi](https://discourse.nodered.org/u/SubhanShamsi)\
**Post date:** [11 July 2021 17:52 UTC](https://discourse.nodered.org/t/cant-differentiate-data-from-different-topics-while-inserting-data-into-my-sql-database/48275/7 "2021-07-11T17:52:54Z")

</div>

![err3](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/6/a6252e9811596acee3e2ed86fc3e9650347e75a7.jpeg)

 ![err4](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/1/91f78bf9de20bf9e1b7dd2aedde023820bf176ae.jpeg)

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [11 July 2021 17:53 UTC](https://discourse.nodered.org/t/cant-differentiate-data-from-different-topics-while-inserting-data-into-my-sql-database/48275/8 "2021-07-11T17:53:45Z")

</div>

And what does your database table look like? What columns has it got and which value do you want to be inserted in which column?

---

<div class="post-metadata">

**Author:** ![SubhanShamsi](https://avatars.discourse-cdn.com/v4/letter/s/6bbea6/32.png) [@SubhanShamsi](https://discourse.nodered.org/u/SubhanShamsi)\
**Post date:** [11 July 2021 18:12 UTC](https://discourse.nodered.org/t/cant-differentiate-data-from-different-topics-while-inserting-data-into-my-sql-database/48275/9 "2021-07-11T18:12:55Z")

</div>

![err5](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/0/7088e1f2b2e4597bd78625b8db8a6b053ca68773.jpeg)

i want to insert into two column

1. Flowrate
2. Totalizer

These two values are in join node.

Here is a function to insert into database

var flow = msg.payload[0];  
var totaliz = msg.payload[1];  
msg.topic = "insert into metertable values ('','','"+flow+"','"+totaliz+"');"  
return msg;

but its now working.

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [11 July 2021 18:21 UTC](https://discourse.nodered.org/t/cant-differentiate-data-from-different-topics-while-inserting-data-into-my-sql-database/48275/10 "2021-07-11T18:21:24Z")

</div>

> [@SubhanShamsi](#):
>
> var flow = msg.payload[0];  
> var totaliz = msg.payload[1];

Change that for:

```auto
var flow = msg.payload["esp32/Flow"];
var totaliz = msg.payload["esp32/Totalizer"];

```

This is because your `msg.payload` in an object not an array, and uses the original messages' `topic` property as the key to the corresponding value.

---

<div class="post-metadata">

**Author:** ![SubhanShamsi](https://avatars.discourse-cdn.com/v4/letter/s/6bbea6/32.png) [@SubhanShamsi](https://discourse.nodered.org/u/SubhanShamsi)\
**Post date:** [11 July 2021 18:53 UTC](https://discourse.nodered.org/t/cant-differentiate-data-from-different-topics-while-inserting-data-into-my-sql-database/48275/11 "2021-07-11T18:53:11Z")

</div>

Thanks alot it working now bye repalce this "["esp32/Flow"];".

I have an another problem is that i use my sql data database auto function of current\_timestamp() for time reading but its show "0000000000" as you see in screenshot.

 ![err6](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/c/2ce76c141db4c7d47ff7fb6260ebce55f3c5374a.jpeg)

---

<div class="post-metadata">

**Author:** ![burtonboucher](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/burtonboucher/32/7806_2.png) [@burtonboucher](https://discourse.nodered.org/u/burtonboucher)\
**Post date:** [17 July 2021 16:58 UTC](https://discourse.nodered.org/t/cant-differentiate-data-from-different-topics-while-inserting-data-into-my-sql-database/48275/12 "2021-07-17T16:58:20Z")

</div>

Looks like your database default is not working or you are passing in a null time by including all four parameters in sql. "insert into metertable values ('','','"+flow+"','"+totaliz+"');"

Test db by inserting a record manually. Does it get a timestamp?  
Fix that or

Create a timestamp and include in your insert statement.

---

<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:** [17 July 2021 18:12 UTC](https://discourse.nodered.org/t/cant-differentiate-data-from-different-topics-while-inserting-data-into-my-sql-database/48275/13 "2021-07-17T18:12:56Z")

</div>

Does INSERT INTO metertable (Flowrate, Totalizer) VALUES ( blah, blah ) create a record with the default timestamp value?

---

<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:** [15 September 2021 18:13 UTC](https://discourse.nodered.org/t/cant-differentiate-data-from-different-topics-while-inserting-data-into-my-sql-database/48275/14 "2021-09-15T18:13:09Z")

</div>

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