# JDBC Input Multiple sql statements as sub-query

**URL:** <https://discuss.elastic.co/t/jdbc-input-multiple-sql-statements-as-sub-query/231787>\
**Category:** Logstash\
**Created:** [May 8, 2020, 7:22pm UTC](https://discuss.elastic.co/t/jdbc-input-multiple-sql-statements-as-sub-query/231787 "2020-05-08T19:22:30Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![myersman](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/myersman/32/67990_2.png) [@myersman](https://discuss.elastic.co/u/myersman)\
**Post date:** [May 8, 2020, 7:22pm UTC](https://discuss.elastic.co/t/jdbc-input-multiple-sql-statements-as-sub-query/231787/1 "2020-05-08T19:22:30Z")

</div>

I want to know if the jdbc input plugin allows me to query multiple tables where the second table runs only for a key value from the first. I presume the config would look something like...

```auto
input {
  jdbc {
    jdbc_driver_library => "c:\path\to\logstash\logstash-core\lib\jars\sqljdbc42.jar"
    jdbc_driver_class => "com.microsoft.sqlserver.jdbc.SQLServerDriver"
    jdbc_connection_string => "jdbc:sqlserver://localhost:1433;databaseName=databaseName"
    jdbc_user => "username"
    jdbc_password => "password"
    statement => "select * from table1"
	type => "table1"
  }
  jdbc {
    jdbc_driver_library => "c:\path\to\logstash\logstash-core\lib\jars\sqljdbc42.jar"
    jdbc_driver_class => "com.microsoft.sqlserver.jdbc.SQLServerDriver"
    jdbc_connection_string => "jdbc:sqlserver://localhost:1433;databaseName=databaseName"
    jdbc_user => "username"
    jdbc_password => "password"
    statement => "select * from table2 where parentid=" <<<table1.id>>>
	type => "table2"
  }
}
output {
  elasticsearch {
    hosts => ["http://localhost:9200"]
    index => "firstindex"
    #user => "elastic"
    #password => "changeme"
  }
}

```

Is this possible? If not, is there a good alternative?

---

<div class="post-metadata">

**Author:** ![elasticforme](https://avatars.discourse-cdn.com/v4/letter/e/f05b48/32.png) [@elasticforme](https://discuss.elastic.co/u/elasticforme)\
**Post date:** [May 8, 2020, 8:19pm UTC](https://discuss.elastic.co/t/jdbc-input-multiple-sql-statements-as-sub-query/231787/2 "2020-05-08T20:19:49Z")

</div>

I don't think this is valid logic

basically when you run two input-jdbc two different record comes to logstash.  
there is no way second jdbc will know field value from one input.

I think you can do filter called "jdbc\_streaming" which can run another jdbc in filter section.  
You have to test it to see if it works or not.

---

<div class="post-metadata">

**Author:** ![Rahul\_Kumar4](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rahul_kumar4/32/67369_2.png) [@Rahul\_Kumar4](https://discuss.elastic.co/u/Rahul_Kumar4)\
**Post date:** [May 10, 2020, 11:07am UTC](https://discuss.elastic.co/t/jdbc-input-multiple-sql-statements-as-sub-query/231787/3 "2020-05-10T11:07:01Z")

</div>

@myersman If your purpose here is to output rows only from `table2` to your `elasticsearch` output then you can try filtering them in a single jdbc input block instead of using two.

```auto
 jdbc {
    jdbc_driver_library => "c:\path\to\logstash\logstash-core\lib\jars\sqljdbc42.jar"
    jdbc_driver_class => "com.microsoft.sqlserver.jdbc.SQLServerDriver"
    jdbc_connection_string => "jdbc:sqlserver://localhost:1433;databaseName=databaseName"
    jdbc_user => "username"
    jdbc_password => "password"
    statement => "SELECT * from table2 WHERE table2.parentid IN (SELECT table1.id FROM table1)"
  }
```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [June 7, 2020, 11:10am UTC](https://discuss.elastic.co/t/jdbc-input-multiple-sql-statements-as-sub-query/231787/4 "2020-06-07T11:10:47Z")

</div>

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