Combine INSERT and SELECT in one SQL query (Zapier

2019-07-12 20:18发布

问题:

I'm crafting Zapier task to insert entry in MySQL if there is no entry with specified unique key or do nothing.

I need to try to insert new row into some table, but if email of new entry is already INSERT will silently throw warning (due to IGNORE keyword).

INSERT IGNORE users(email, hashed_password)
VALUES ('<email>`, '<some_hashed_password>')

But in both scenarios my query is not returning anything and Zapier ends task with this message:

Question: Is there some way to have one complex SQL command that will combine INSERT and SELECT so with one query I will get some result set from from DB, not empty object or whatever INSERT returns?

P.S. This works in MySQL:

INSERT IGNORE reporting.users(`email`, `password`)
VALUES ("test@test.ts", "test");
SELECT * FROM reporting.users as u WHERE u.email = "test@test.ts";

but this consists of two queries and this doesn't work in Zapier.

回答1:

This is an old question but I was grappling with the same issue today. In trying to find a solution I came across this qn and so when I found a solution / work-around I thought I'd do the decent thing and post back...

Based on the red "Bargle" error in their post I believe zmii must have been trying to use the MySQL custom search query. Zapier has to have an output from the query or it faults. I did some looking around and crafted my custom query thus:

SELECT IFNULL( (SELECT employee_id FROM timesheets.employees WHERE employee_id = <Step 6 | sheet_data_id> LIMIT 1) ,0) AS result; based on the selected answer here. So, my query will output an employee ID if it is found and 0 if not. I then inserted a Zapier PATHS step which I based on the output 'result' from my custom query. If the result is greater than 0 I update an existing record. If it is 0 then I insert a new record. I suspect I could use the custom query code to do the branching and updating/inserting but I didn't try that yet as I have other things to try first.

Edit Actually I have had to revise this answer based on my conversation with Zapier here. I've retained the original answer but hidden it. The syntax works but only if the query is a SELECT query, it will not work for an INSERT or UPDATE query. See the discussion in the comments of the linked question for details. Essentially it is not possible to do an insert or update operation via the Zapier MySQL Custom Query step at this stage.



标签: mysql sql zapier