简体   繁体   中英

MySQL Stored Procedure Conditional Statement

Can anyone help me on how could I have an conditional statement on the MySQL stored procedure?

I have this as sample query,

If my stored procedure parameter isFirstTask = true then I will use LEFT JOIN else I will use INNER JOIN

SELECT *
FROM jobdetails jd
INNER JOIN taskslogs tl ON jd.jobID = tl.jobid;

The question is how could change the INNER JOIN into LEFT JOIN without repeating the whole query just to replace one word. Assuming that my query is bulk. Is it possible? Some idea please.

SELECT *
FROM jobdetails jd
      LEFT JOIN taskslogs tl ON jd.jobID=tl.jobid
WHERE IF(isFirstTask = TRUE, TRUE, tl.jobid IS NOT NULL);

you can build the query as a string inside the stored procedure. While building the query, check the value of your parameter and decide which JOIN to use. Hope this helps.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM