How to use one variable value as a result for another variable input in Grafana?

I am using MySQL as my data source in Grafana and I am executing SELECT Queries to fetch data

Here is the query that I am executing,

SELECT * FROM [[databases]].[[columns]] limit 0, [[limit]];

I have created 3 variables “databases” , “columns” and “limit” which will give me the appropriate result and I have variablized “databases” and “limit values”

But what I want is when I select the value of database from drop down, the column variable should show tables from selected database only

What should be my variable query for that?

2 Likes

In the configuration of the [[columns]] template variable, you can have the options in the drop-down be populated by a SQL query (https://grafana.com/docs/reference/templating/#variable-types). In that query itself you can use the [[databases]] variable to run a query that would return the desired set of values for [[columns]]. You may have to experiment with the variable recalculation option, to ensure that the values of [[columns]] are recalculated whenever the user changes their selection of [[databases]].

In [[column]] template variable I am using “show tables;” SQL query.
but when I try to save it, I got an error message like this “Template variables could not be initialized: Error 1046: No database selected

In [[Database]] template variable I am using “show databases;” SQL query, which works fine and gives me the proper result of the list of databases

Can you please give me an example of how can I merge [[database]] template variable value to [[column]] template variable

I figured it out

I changed my [[columns]] template variable query to “show tables from $databases” and it worked

1 Like

Right, that’s the one. Glad you worked it out!

Thank you for your suggestions

hello,

I found one solution.

I had this problem , get host name in two format.

prometheus query:
variable= host
query= label_values(rsh_node_time{job=~"$job"}, hostname)

result host=“node1.sip.com

but, Now, do do another query to mysql , I do need only “node1”

need host and hostmysql

new variable entry:
variable=hostmysql
query= label_values(rsh_node_time{job=~"$job",hostname="$host"}, hostname)
regex= /(.*)(.sip.com |.sip2.com)/

the key is to pass hostname="$host" and get label hostname.

and now , we do have access to regex.

regards!
Valdemar

Dear Valdemar,

I have a similar situation in which am trying to use one variable output to another but seems not working ,Am using InfluxDB

This is the variable
SHOW TAG VALUES WITH KEY=NodeName WHERE NodeName =~ /AJB/ → $cust

Query

SELECT last(“SiteStatus”) as STATE FROM “Bucket” WHERE “agent_host” = ‘NMS1’ AND ( SiteStatus = 2 OR SiteStatus = 3) AND time >= now() -1m GROUP BY NodeName

Any suggesions how to call this variable?

Thanks in advance
Regards
Danish