# How to run INSERT SQL query depending upon the result of SELECT query?

**URL:** https://discourse.nodered.org/t/how-to-run-insert-sql-query-depending-upon-the-result-of-select-query/50200
**Category:** General
**Created:** [26 August 2021 08:30 UTC](https://discourse.nodered.org/t/how-to-run-insert-sql-query-depending-upon-the-result-of-select-query/50200 "2021-08-26T08:30:36Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Nirajan](https://avatars.discourse-cdn.com/v4/letter/n/6bbea6/32.png) [@Nirajan](https://discourse.nodered.org/u/Nirajan)
#### Post date: [26 August 2021 08:30 UTC](https://discourse.nodered.org/t/how-to-run-insert-sql-query-depending-upon-the-result-of-select-query/50200/1 "2021-08-26T08:30:36Z")

</div>

Hello Everyone,  
Is there a possibility to write more than one query in a function node and execute it on single instance of mysql node?. First, i would like to run a select query and then run an insert query only if the result from the first query is not null.

It seems like i need to write individual queries in separate function nodes and also create separate mysql nodes for each function node. Please correct me if my approach is wrong.

I am eagerly looking for a better approach. Any help would be appreciated.

Thanks

---

<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: [26 August 2021 08:52 UTC](https://discourse.nodered.org/t/how-to-run-insert-sql-query-depending-upon-the-result-of-select-query/50200/2 "2021-08-26T08:52:43Z")

</div>

Are you trying to do an insert but only if the row does not already exist? If so then Google for  
Mysql insert if not exists  
And you will find a number of solutions.

---

<div class="post-metadata">

### Author: ![Nirajan](https://avatars.discourse-cdn.com/v4/letter/n/6bbea6/32.png) [@Nirajan](https://discourse.nodered.org/u/Nirajan)
#### Post date: [26 August 2021 09:12 UTC](https://discourse.nodered.org/t/how-to-run-insert-sql-query-depending-upon-the-result-of-select-query/50200/3 "2021-08-26T09:12:05Z")

</div>

Hello Colin,

Thank you very much for your suggestion.  
Actually i want to retrieve a column data from a table through SELECT query and then insert this column data as a value into another table through INSERT query. But i do not want to insert to another table if the column data from first table is empty.

Regards,

---

<div class="post-metadata">

### Author: ![harmonic7](https://avatars.discourse-cdn.com/v4/letter/h/b5a626/32.png) [@harmonic7](https://discourse.nodered.org/u/harmonic7)
#### Post date: [26 August 2021 21:10 UTC](https://discourse.nodered.org/t/how-to-run-insert-sql-query-depending-upon-the-result-of-select-query/50200/4 "2021-08-26T21:10:36Z")

</div>

Hi @Nirajan ,

I'm not sure if this will help or not, but if it were a SQL Server database I'd write something like:

```sql
INSERT INTO DestinationTable (TheOnlyColumn)
    SELECT TheOnlyColumn
    FROM SourceTable
    WHERE TheOnlyColumn IS NOT NULL

```

With this approach, no insert will take place unless the column from the source table is not null.

But, perhaps I haven't understood your requirements fully?

---

<div class="post-metadata">

### Author: ![Nirajan](https://avatars.discourse-cdn.com/v4/letter/n/6bbea6/32.png) [@Nirajan](https://discourse.nodered.org/u/Nirajan)
#### Post date: [27 August 2021 07:33 UTC](https://discourse.nodered.org/t/how-to-run-insert-sql-query-depending-upon-the-result-of-select-query/50200/5 "2021-08-27T07:33:25Z")

</div>

Hallo,

Thanks a lot for your help and for a possible solution.  
I will give it a try. I am hoping this will work out.

Best Regards,

---

<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: [26 October 2021 07:33 UTC](https://discourse.nodered.org/t/how-to-run-insert-sql-query-depending-upon-the-result-of-select-query/50200/6 "2021-10-26T07:33:26Z")

</div>

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