I am writing a stored procedure where I am fetching data from one table and based on that data I want to use that data in other manipulations. Eg
select id,name,email,mobile,user_type from users
And based on the user_type I want to do further processing using id field . for Eg,
if(user_type=1)
begin
select * from some_table where Userid = id
End
Else if (user_type=2)
begin
select * from some_other_table where Userid = id
End
Can anyone provide me with basic syntax to do that
You can declare
variables in the procedure and then fill them using SELECT ... INTO ... FROM
statement. Then those variables can be used within IF THEN ELSE
expressions.
DELIMITER $$
CREATE PROCEDURE test()
BEGIN
DECLARE temp_id, temp_user_type INT;
DECLARE temp_name, temp_email, temp_mobile VARCHAR(100);
SELECT id,name,email,mobile,user_type
INTO temp_id, temp_name, temp_email, temp_mobile, temp_user_type FROM users WHERE id = 1;
IF temp_user_type = 1 THEN
SELECT * FROM some_table WHERE Userid = temp_id
ELSEIF temp_user_type = 2 THEN
SELECT * FROM some_other_table WHERE Userid = temp_id
ELSE
--
END IF;
END$$
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.