Best answer

Run a custom MySQL query

  • 15 March 2022
  • 6 replies
  • 665 views

Hi,

I’m trying to update some rows in a table with a query similar to this:

UPDATE my_table
SET my_descriptive_column = "new content", updated_at = now()
WHERE created_at > "2022-01-01" AND my_type = "some_type";

...which I added to a MySQL action with the action event set to “Find rows via custom query” because I couldn’t find a better action to put this in. It errors-out because this query doesn’t return any rows. Is there another action that I can put this under that’ll allow this SQL to run?
I thought as an alternative I’d try a loop, preceded by a regular `SELECT * FROM` custom query action. But the loop only ever runs one row. I set it up like this...

I might not be understanding how to set up loops correctly.

tl;dr

How can one run an UPDATE SQL query? If one can’t, what am I doing wrong in the loop above?

Thank you!

 

icon

Best answer by jesse 19 July 2022, 23:32

View original

This post has been closed for comments. Please create a new post if you need help or have a question about this topic.

6 replies

Userlevel 7
Badge +14

Hi @nicolas_goosen 

Good question.

To clarify the Zap action will return 1 row.

You wouldn’t need Looping if only 1 row is returned.

 

So does any custom query always return only one row? Even if I don’t add a “LIMIT” clause?

Userlevel 7
Badge +14

@nicolas_goosen 

Likely only 1 row will be returned as indicated by the name of the Zap action: Find Row

Most Zap triggers/actions work on a per record basis.

You can open a ticket with Zapier Support for official confirmation: https://zapier.com/app/get-help

I think I know what to do now. I’ll create a loop that repeats Find Row → Update Row on each row. Wow, that’ll be a serious performance hit on the DB and also use up a lot of actions. 

It’s rather silly that one can’t just run a regular UPDATE...WHERE query. I’ll open a ticket like you suggest, to confirm.

Thanks!

Userlevel 7
Badge +9

Thanks for reaching out, @nicolas_goosen! Definitely appreciate you sharing your workaround with the Community too!

I wanted to pop in and share what support had mentioned in their email to you in case it’s helpful at all.
 

The Find Row by Custom Query Action is intended to be used with the WHERE condition to extract a row that matches a certain search criteria. It is not intended to run an UPDATE query.

 

Userlevel 7
Badge +9

Hey folks! To clarify the answer to this question, as Troy mentioned, most Zap triggers/actions work on a per record basis. That means the Find Row action will only return a single row. Because of that, Looping likely isn’t necessary here. 

It sounds like you were going to move forward by creating a loop that repeats Find Row → Update Row on each row. However, you expressed some concerns:

Wow, that’ll be a serious performance hit on the DB and also use up a lot of actions. 

It’s rather silly that one can’t just run a regular UPDATE...WHERE query.

To which our support team replied:

The Find Row by Custom Query Action is intended to be used with the WHERE condition to extract a row that matches a certain search criteria. It is not intended to run an UPDATE query.