# MySQL statement help

**URL:** <https://discourse.nodered.org/t/mysql-statement-help/19923>\
**Category:** General\
**Created:** [5 January 2020 10:03 UTC](https://discourse.nodered.org/t/mysql-statement-help/19923 "2020-01-05T10:03:19Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mark\_Ellis](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mark_ellis/32/79062_2.png) [@Mark\_Ellis](https://discourse.nodered.org/u/Mark_Ellis)\
**Post date:** [5 January 2020 10:03 UTC](https://discourse.nodered.org/t/mysql-statement-help/19923/1 "2020-01-05T10:03:19Z")

</div>

Can some help me with this sql statement which i want to insert into a function node, i would like also to use variables inplace 1 @ State and LRoom1 @ Device, but not yet.

msg.topic="UPDATE MultiSenser SET State = 1 WHERE Device = LRoom1";  
return msg;

any help much appreciated

---

<div class="post-metadata">

**Author:** ![ghayne](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ghayne/32/39_2.png) [@ghayne](https://discourse.nodered.org/u/ghayne)\
**Post date:** [5 January 2020 10:16 UTC](https://discourse.nodered.org/t/mysql-statement-help/19923/2 "2020-01-05T10:16:43Z")

</div>

Welcome to the forum. A change node would also work:

 ![Screenshot_2020-01-05_10-15-18](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/e/eee9b5bc5dc806025e673b74bed37fded7b62447.png)

---

<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:** [5 January 2020 10:35 UTC](https://discourse.nodered.org/t/mysql-statement-help/19923/3 "2020-01-05T10:35:11Z")

</div>

You need single quotes round LRoom1 if that is a string rather than a column name.

---

<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:** [5 January 2020 10:37 UTC](https://discourse.nodered.org/t/mysql-statement-help/19923/4 "2020-01-05T10:37:31Z")

</div>

If using a Change node then I don't think you should have the double quotes round the outside. The fact that the type is selected as a/z means that it knows it is a string. I don't think the mysql node needs the ; on the end of the statement, it will add that for you.

---

<div class="post-metadata">

**Author:** ![Mark\_Ellis](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mark_ellis/32/79062_2.png) [@Mark\_Ellis](https://discourse.nodered.org/u/Mark_Ellis)\
**Post date:** [5 January 2020 10:37 UTC](https://discourse.nodered.org/t/mysql-statement-help/19923/5 "2020-01-05T10:37:57Z")

</div>

Thanks for reply, i would of never thought of using Change node, but the statement to insert is  
UPDATE MultiSenser SET State = 1 WHERE Device = "LRoom1"

Thanks very much, but i cant use variables with a change node.

---

<div class="post-metadata">

**Author:** ![Mark\_Ellis](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mark_ellis/32/79062_2.png) [@Mark\_Ellis](https://discourse.nodered.org/u/Mark_Ellis)\
**Post date:** [5 January 2020 10:44 UTC](https://discourse.nodered.org/t/mysql-statement-help/19923/6 "2020-01-05T10:44:20Z")

</div>

Hi Colin Thanks for your reply, LRoom1 is row primary key for a row from Device column.

---

<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:** [5 January 2020 10:50 UTC](https://discourse.nodered.org/t/mysql-statement-help/19923/7 "2020-01-05T10:50:18Z")

</div>

To insert variables in a string (if using a function node) you can use the new(ish) string template literals in javascript. [https://appdividend.com/2019/01/23/javascript-template-literals-example-javascript-string-interpolation/](https://appdividend.com/2019/01/23/javascript-template-literals-example-javascript-string-interpolation/)

I think you could also do it in a Change node using JSONata but I will leave it for someone else to tell you how to do that.

---

<div class="post-metadata">

**Author:** ![dynamicdave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dynamicdave/32/96_2.png) [@dynamicdave](https://discourse.nodered.org/u/dynamicdave)\
**Post date:** [5 January 2020 10:57 UTC](https://discourse.nodered.org/t/mysql-statement-help/19923/8 "2020-01-05T10:57:50Z")

</div>

A tip I was given, which works really well for me, is to use the 'template' node (located in the function category) as it makes constructing the MySQL query fairly easy. Here's a very simple example.

![ScreenShot063](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/b/b0b02a155dce6b2adbf7ba1a8d27ea722bea1a00.png)

 ![ScreenShot062](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/b/b10fcc088b3f7827e7c19aa5d5a027bf150a5365.png)

---

<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:** [5 January 2020 12:16 UTC](https://discourse.nodered.org/t/mysql-statement-help/19923/9 "2020-01-05T12:16:36Z")

</div>

If you need to run the same update statement lots of times with different variables, have a look at SQL prepared statements as they are a lot more efficient.
