# SQL Code Help

**URL:** <https://forum.inductiveautomation.com/t/sql-code-help/4805>\
**Category:** Ignition\
**Created:** [July 11, 2012, 8:18pm UTC](https://forum.inductiveautomation.com/t/sql-code-help/4805 "2012-07-11T20:18:34Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jlr573](https://avatars.discourse-cdn.com/v4/letter/j/c6cbf5/32.png) [@jlr573](https://forum.inductiveautomation.com/u/jlr573)\
**Post date:** [July 11, 2012, 8:18pm UTC](https://forum.inductiveautomation.com/t/sql-code-help/4805/1 "2012-07-11T20:18:34Z")

</div>

I’m trying to show the results of a query in a table using information from different tables.  
Table A columns: id, state  
Table B columns: id, batch\_id, state

Table A Records:  
1 - Batch Ready  
2 - Batch Completed  
3 - Bad Batch

When a batch is created a new record is placed into table C, under the state column we are putting the id number that matches the current state (encase the description changes).

Then we show table C and do some filtering based on the states and other things, the only problem is that nobody can remember what the state numbers mean.

Is there a way to pull the state description from table A and replace the id value with the states description text? and show that to the operator

---

<div class="post-metadata">

**Author:** ![Greg.Buehler](https://avatars.discourse-cdn.com/v4/letter/g/ecc23a/32.png) [@Greg.Buehler](https://forum.inductiveautomation.com/u/Greg.Buehler)\
**Post date:** [July 11, 2012, 8:27pm UTC](https://forum.inductiveautomation.com/t/sql-code-help/4805/2 "2012-07-11T20:27:00Z")

</div>

So basically you just want to join on the status field?

```auto
SELECT *
FROM Batches
INNER JOIN States ON Batches.state = States.id;
```

---

<div class="post-metadata">

**Author:** ![nathan](https://avatars.discourse-cdn.com/v4/letter/n/c5a1d2/32.png) [@nathan](https://forum.inductiveautomation.com/u/nathan)\
**Post date:** [July 12, 2012, 11:13am UTC](https://forum.inductiveautomation.com/t/sql-code-help/4805/3 "2012-07-12T11:13:26Z")

</div>

Check out [SQL Zoo](http://sqlzoo.net/). What you’re asking for is very possible and JOINs aren’t as hard as they may look. You could even use a GUI query builder and copy/paste the query into Ignition.

Based on your description, I think your query would look something like this:

```auto
SELECT C.*, B.state FROM B, C WHERE c.id = b.id
```

or (like Greg suggested with the JOIN ON syntax).

You might also want to use table\_name.columname AS your\_desired\_alias immediately after the SELECT.

---

<div class="post-metadata">

**Author:** ![jlr573](https://avatars.discourse-cdn.com/v4/letter/j/c6cbf5/32.png) [@jlr573](https://forum.inductiveautomation.com/u/jlr573)\
**Post date:** [July 12, 2012, 12:17pm UTC](https://forum.inductiveautomation.com/t/sql-code-help/4805/4 "2012-07-12T12:17:02Z")

</div>

Thanks guys, I’m pretty green when it comes to SQL and I usually find what I need on the web and I thought the INNER JOIN was what I needed but couldn’t get it to work. But It works now thanks for the help.
