# Merging columns from different tables into one table

**URL:** <https://forum.inductiveautomation.com/t/merging-columns-from-different-tables-into-one-table/73217>\
**Category:** Ignition\
**Tags:** perspective, database\
**Created:** [May 3, 2023, 6:10pm UTC](https://forum.inductiveautomation.com/t/merging-columns-from-different-tables-into-one-table/73217 "2023-05-03T18:10:38Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cesar\_Jimenez](https://avatars.discourse-cdn.com/v4/letter/c/6a8cbe/32.png) [@Cesar\_Jimenez](https://forum.inductiveautomation.com/u/Cesar_Jimenez)\
**Post date:** [May 3, 2023, 6:10pm UTC](https://forum.inductiveautomation.com/t/merging-columns-from-different-tables-into-one-table/73217/1 "2023-05-03T18:10:38Z")

</div>

I have 4 tables that is recording a value (pressure) every 30 seconds (t\_stamp). I would like to join the pressures from each table into one single one. I have tried using the union statement, but it seems to join everything into one single column, instead of multiple ones. I am new when it comes to MySQL and would appreciate some help. Thanks!

 ![SS5](https://us1.discourse-cdn.com/inductiveautomation/original/3X/c/8/c8f72d8fdfa5212eac3d6e36b9b8bc03fc3282b4.png)

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [May 3, 2023, 6:31pm UTC](https://forum.inductiveautomation.com/t/merging-columns-from-different-tables-into-one-table/73217/2 "2023-05-03T18:31:19Z")

</div>

Try joining them on the t\_stamp instead of union.

---

<div class="post-metadata">

**Author:** ![Cesar\_Jimenez](https://avatars.discourse-cdn.com/v4/letter/c/6a8cbe/32.png) [@Cesar\_Jimenez](https://forum.inductiveautomation.com/u/Cesar_Jimenez)\
**Post date:** [May 3, 2023, 6:45pm UTC](https://forum.inductiveautomation.com/t/merging-columns-from-different-tables-into-one-table/73217/3 "2023-05-03T18:45:22Z")

</div>

This is how I had it, can you walk me through what you would do?

---

<div class="post-metadata">

**Author:** ![dkhayes117](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dkhayes117/32/68344_2.png) [@dkhayes117](https://forum.inductiveautomation.com/u/dkhayes117)\
**Post date:** [May 3, 2023, 6:53pm UTC](https://forum.inductiveautomation.com/t/merging-columns-from-different-tables-into-one-table/73217/4 "2023-05-03T18:53:22Z")

</div>

I usually don't join on a t\_stamp. Also, I don't know how much time difference you have in the time values of each table. Either use `date_format` to trim off the milliseconds or use an interval like below.

```auto
select 
    t1.value, t2.value, t3.value, t4.value, t1.t_stamp
from table1 t1
    join table2 t2 on t2.t_stamp between t1.t_stamp - INTERVAL 1 SECOND and t1.t_stamp + INTERVAL 1 SECOND
    join table3 t3 on t3.t_stamp between t1.t_stamp - INTERVAL 1 SECOND and t1.t_stamp + INTERVAL 1 SECOND
    join table4 t4 on t4.t_stamp between t1.t_stamp - INTERVAL 1 SECOND and t1.t_stamp + INTERVAL 1 SECOND

where 
    date(t1.t_stamp) = curdate()

```

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [May 3, 2023, 8:05pm UTC](https://forum.inductiveautomation.com/t/merging-columns-from-different-tables-into-one-table/73217/5 "2023-05-03T20:05:00Z")

</div>

Yes, I'd only join on the t\_stamp if they follow the same pattern in each table. Otherwise it's... ugly ?
