# Read data form two tables at the same time

**URL:** https://discourse.nodered.org/t/read-data-form-two-tables-at-the-same-time/25186
**Category:** General
**Created:** [20 April 2020 11:19 UTC](https://discourse.nodered.org/t/read-data-form-two-tables-at-the-same-time/25186 "2020-04-20T11:19:56Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![Ahd](https://avatars.discourse-cdn.com/v4/letter/a/e9a140/32.png) [@Ahd](https://discourse.nodered.org/u/Ahd)
#### Post date: [20 April 2020 11:19 UTC](https://discourse.nodered.org/t/read-data-form-two-tables-at-the-same-time/25186/1 "2020-04-20T11:19:56Z")

</div>

Hi ALL,  
I am wondering if it is possible to make an " inner join" between two tables in the function node. For example, I have two tables and I want to display the data from both tables at a certain condition( if id=id) as shown below :

msg.topic=  
"SELECT \* FROM Check\_Out JOIN Check\_IN ON Check\_Out.id = Check\_IN.id";  
return msg;

Is that possible?

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [20 April 2020 11:32 UTC](https://discourse.nodered.org/t/read-data-form-two-tables-at-the-same-time/25186/2 "2020-04-20T11:32:14Z")

</div>

> [@Ahd](#):
>
> two tables in the function node

SQL operations - in a JS function? What/Where is this table (do you already have data from the DB)? What type of database are you referring to?

> [@Ahd](#):
>
> msg.topic=  
> "SELECT \* FROM Check\_Out JOIN Check\_IN ON Check\_Out.id = Check\_IN.id";  
> return msg;
> 
> Is that possible?

Of course setting `msg.topic` to a SQL like string is possible (you can set a string to anything stringy)

EDIT...  
Let guess as you haven't provided details...  
you're working with MYSQL? And you wish to know if you can send a SQL string (in `msg.topic`) that contains a SQL JOIN and get data back from MYSQL?

Give it a go & see if it works.

---

<div class="post-metadata">

### Author: ![Ahd](https://avatars.discourse-cdn.com/v4/letter/a/e9a140/32.png) [@Ahd](https://discourse.nodered.org/u/Ahd)
#### Post date: [20 April 2020 12:10 UTC](https://discourse.nodered.org/t/read-data-form-two-tables-at-the-same-time/25186/3 "2020-04-20T12:10:09Z")

</div>

well , my story is that i I have two tables in my database first is "check in" and the second is "checkout" as shown below. I have access to these tables from node-red. What I want to do is to make the name, time in and time out in one message (under one payload )on the node-red . As I have a name filed in each table as PK.

So I tried to make that by writing the following query in the function node but it showed nothing.

msg.topic=  
"SELECT \* FROM Checkin JOIN Checkout ON checkin.name= Checkout.name";  
return msg;

table 1and table 2:  
 ![table1](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/5/55f0c3e824860dafb7183b5b9151ad4c0debb4de.png)  
 ![table2](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/4/44caaaf29e6033d4fd71a96fadd736968ba76203.png)

---

<div class="post-metadata">

### Author: ![Ahd](https://avatars.discourse-cdn.com/v4/letter/a/e9a140/32.png) [@Ahd](https://discourse.nodered.org/u/Ahd)
#### Post date: [20 April 2020 12:22 UTC](https://discourse.nodered.org/t/read-data-form-two-tables-at-the-same-time/25186/4 "2020-04-20T12:22:33Z")

</div>

@Steve-Mcl

thanks, it solved

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [20 April 2020 12:26 UTC](https://discourse.nodered.org/t/read-data-form-two-tables-at-the-same-time/25186/5 "2020-04-20T12:26:44Z")

</div>

Glad you solved it.

It is considered good forum etiquette to you post what you did for others to benefit from.

If you do, don't forget to list any external nodes you use in the solution (e.g. node-red-contrib-some\_database\_sql\_node)

---

<div class="post-metadata">

### Author: ![Ahd](https://avatars.discourse-cdn.com/v4/letter/a/e9a140/32.png) [@Ahd](https://discourse.nodered.org/u/Ahd)
#### Post date: [20 April 2020 13:20 UTC](https://discourse.nodered.org/t/read-data-form-two-tables-at-the-same-time/25186/6 "2020-04-20T13:20:31Z")

</div>

Sure, I dont mind to share the information .

here is the flow 🙂

 ![flow](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/7/473946791ec27360b8d3c931be956bc11d9a56e3.png)

```auto
[{"id":"b2105280.bde26","type":"tab","label":"Flow 5","disabled":false,"info":""},{"id":"92c4141e.80e058","type":"function","z":"b2105280.bde26","name":"read from two tables","func":"msg.topic= \n\"SELECT * FROM Checkin JOIN checkout ON Checkin.name = checkout.name\"; \nreturn msg;\n\n//msg.topic=\"select * from Check_Out \"","outputs":1,"noerr":0,"x":420,"y":200,"wires":[["be7ba06.dbe766"]]},{"id":"be7ba06.dbe766","type":"mysql","z":"b2105280.bde26","mydb":"2cd1ee93.18a152","name":"DB","x":610,"y":200,"wires":[["82f0348b.fe9fb8"]]},{"id":"ccd72ada.f45928","type":"inject","z":"b2105280.bde26","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":220,"y":200,"wires":[["92c4141e.80e058"]]},{"id":"82f0348b.fe9fb8","type":"debug","z":"b2105280.bde26","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":790,"y":200,"wires":[]},{"id":"2cd1ee93.18a152","type":"MySQLdatabase","z":"","name":"yyyyyyyyyyyyyyyy","host":"XXXXXXXX","port":"3306","db":"XXXXXXX","tz":""}]

```

type or paste code here

```auto

```

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [20 April 2020 14:02 UTC](https://discourse.nodered.org/t/read-data-form-two-tables-at-the-same-time/25186/7 "2020-04-20T14:02:50Z")

</div>

You didnt say what the node type was (exact contrib name please - you get it from the pallet) - without the full name, others cant replicate what you have.

---

<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: [4 May 2020 14:03 UTC](https://discourse.nodered.org/t/read-data-form-two-tables-at-the-same-time/25186/8 "2020-05-04T14:03:00Z")

</div>

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