Using an expression from a MySQL query doesn't work good

Hello everybody!
In my scenario, I check the existence of a record on a MySQL database. Then with an If node, if this row exists I do one thing, and if it doesn’t, I do another thing.

But I can’t get the correct value from the MySQL query result. This is the query to MySQL:
FROM comments
WHERE text = ‘Muy bien felicidades’ AND username = ‘xxxxxx’ AND mediaid = ‘123456789’);

and it returns me the value “1”, because this row already exists.

But when I add the expression of this result on the If node, it doesn’t recognize the node reference in the expression, because it doesn’t have a static text, it changes every time. Look at the image:

The text of the query is used as a reference for the MySQL node in the expression. So if the query changes, the reference is lost, and it doesn’t found its value.

How can I solve this?

You have to us an ‘as’ statement like:
select id as userid from tablename

After that the column name is every time the same…


I don’t think this is going to work, because the query will change with every execution…

I need to check the existence of a row with three specific values on the DB. These three specific values will be different in most of the cases. So, since n8n references to the name of the query, I can not reference to it dynamically…

I tried referencing the parameter of the same MySQL node, inside the output reference. But it doesn’t work too. It has different text (the first word “SELECT” is missing, as well as the “;” on the end). I tried also replacing these word via javascript, but it still doesn’t work.

Hey @ruccc!

Did you try removing the quotes? Can you try this expression: {{$node["Search comment on DB"].json[$node["Search comment on DB"].parameter["query"].replace('SELECT','').replace(';','')]}}

Did you try what @BillAlex did suggest? Because I would also assume that it works.

In case you did not understand it correctly. He meant that you should change the query in the MySQL node. If you do that should the value be exported as the value you defined after as. So if you add as doesExist should it not be EXIST(SELECT ... anymore rather just doesExist.

1 Like

Oh, thank you, now I undesrtood it!

Now it’s working. Many thanks! :slight_smile:

1 Like