How to concatenate string from {{ ... }}

I have postgres query

SELECT * FROM "Test" WHERE "UserId" = {{ urlparams.userid }} ORDER BY "id" DESC;

Now I would like to make the WHERE clause conditional so the query will contain WHERE only if urlparams.userid is defined. I've tried to put

{{ urlparams.userid ? 'WHERE "UserId" =' + urlparams.userid : null }} but getting weird errors.

I also tried to create JS query and trigger sql queries conditionally, but I am not sure how to return query data inside JS query.

@Pablo You are trying to do do this?
if (urlparams.userid != "") {
run query
}

If so, this can be done by using the advanced tab

So the query will only run if urlparams.userid is defined and not null.

Hi @ScottR ,

I am not trying to conditionally enable or disable query but rather slightly modify query if userid is null. So when it's null I need to run

SELECT * FROM "Test" ORDER BY "id" DESC;
When it's not null I need to run

SELECT * FROM "Test" WHERE "UserId" = {{ urlparams.userid }} ORDER BY "id" DESC;

OK and is this something you want to be done automatically on page load?
Or is it user event based?

@ScottR
I would like it to be done on page load only for now.

OK so I still believe that you can set the conditional in the advanced tab for each query
So
SELECT * FROM "Test" ORDER BY "id" DESC;
will run when urlparams.userid == ""
and the other query
SELECT * FROM "Test" WHERE "UserId" = {{ urlparams.userid }} ORDER BY "id" DESC;
should run urlparams.userid != ""
Both conditional set in Advanced Tab

and for each query you set it to run automatically when inputs change in the General Tab

But my table component can be bound to only one query. How can I use 2 queries on single table component?

Thanks

In the Data field for that table, do the following
{{urlparams.userid == ""?query1.data:query2.data}}
This checks to see if the userid is blank and if it is populate table with query1.data, but if it is not empty populate table with query2.data
You'll want to set up the separate queries to automatically run when inputs change.
Screen Shot 2021-12-22 at 12.29.17 PM