简体   繁体   中英

If statement shorthand syntax in MYSQL store procedure

CREATE PROCEDURE `new_procedure`()
BEGIN

    INSERT IGNORE INTO users ( group_id, username, password, email) 
    SELECT status, CONCAT(first_name, last_name), password, email FROM another_user;

END

Here is a sample I wanna write status from another_user into group_id in users directly.

Is there any method to do like Java if else shorthand syntax? If can the statement would be like this:

SELECT (status==1?4,5), CONCAT(first_name, last_name), password, email FROM another_user;

It's the IF function:

SELECT IF(status = 1, 4, 5), CONCAT(first_name, last_name), password, email
FROM another_user;

You can also use the standard SQL CASE expression:

SELECT CASE status
        WHEN 1 THEN 4
        ELSE 5
       END,
        CONCAT(first_name, last_name), password, email
FROM another_user;

This is not particular to stored procedures, it's just a general feature of SQL expressions, and you can use it anywhere.

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