0

I want to execute multiple query like this

Delete FROM TableA where CreatedBy="2"
Delete FROM TableB where CreatedBy="2"
Delete FROM TableC where CreatedBy="2"
Delete FROM TableD where CreatedBy="2"

I also want to do more update and insert query using that id.Even id may change.
Every time I have to change the value to do that.
Is there any way declaring variable and execute the quires As example

var userid=2;
Delete FROM TableA where CreatedBy="userid"
Delete FROM TableB where CreatedBy="userid"
Delete FROM TableC where CreatedBy="userid"
Delete FROM TableD where CreatedBy="userid"

Here if I only change one place the id which will change everywhere.
I want to make a sql file and run it to my MySQL server changing the userid.
Is there anyway to do that? and how?

1 Answer 1

2

You can set a session variable this way:

SET @userid := '2';
DELETE FROM `TableA` WHERE `CreatedBy` = @userid;
DELETE FROM `TableB` WHERE `CreatedBy` = @userid;
DELETE FROM `TableC` WHERE `CreatedBy` = @userid;
DELETE FROM `TableD` WHERE `CreatedBy` = @userid;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.