How to do a join query?

I need to query two table with join, anyway to do that in Kong, I’ve looked through Kong’s source code , all examples are single table query.

Hi,

You left some very important details out of your question, like which version of Kong are you using and which database (PostgreSQL vs Cassandra).

The kong version is important because there was a change in the interface we use to communicate with the database changed. Older versions of Kong use the kong.dao library, while newer versions use the kong.db library. The way to do joins depends on which of these libraries you are using, and which database you are using as well.

Assuming that you are using a somewhat recent version of Kong, which has access to kong.db and PostgreSQL, you can execute a random sql query (like a join) like this:

local connector = kong.db.connector
local sql = "SELECT * FROM kong.one_table JOIN kong.other_table WHERE ..."
local rows, err, partial, num_queries = connector:query(sql)

The returned values have the same meaning as the query method in pgmoon.

1 Like

thanks a lot, your solution works ,I am using the latest Kong 0.13.x and PostgreSQL.

hi, where is the kong.db.connector from? I can’t find it.

You mean, where is its source code? The PostgreSQL one is here: