简体   繁体   English

MySQL存储过程基于参数的返回值

[英]MySQL stored procedure return value based on parameter

I'm trying to create a stored procedure in MySql where it will return all users based on the gender parameter and here is my code: 我正在尝试在MySql中创建一个存储过程,该存储过程将根据性别参数返回所有用户,这是我的代码:

DELIMITER $$

USE `my_database`$$

DROP PROCEDURE IF EXISTS `GET_ALL_USERS`$$

CREATE DEFINER=`root`@`localhost` PROCEDURE `GET_ALL_USERS`(IN IN_GENDER CHAR)
    COMMENT 'Get All Users'
BEGIN
    SELECT
        *
    FROM
        users
    WHERE
        gender = IN_GENDER;
END$$

DELIMITER;

This code works when the parameter is set to 'f' or 'm'. 当参数设置为“ f”或“ m”时,此代码有效。 When the gender param is not set it returns nothing. 如果未设置性别参数,则不返回任何内容。 What i want is if the gender param is not set it will return all users. 我想要的是,如果未设置性别参数,它将返回所有用户。 What value should I set to the gender parameter to get all users? 我应该为性别参数设置什么值才能获得所有用户? Please help thanks 请帮忙谢谢

Off the top of my head, not tested... 没经过我的测试,...

Try it like this: 像这样尝试:

gender LIKE CASE WHEN IN_GENDER='' THEN '%' ELSE IN_GENDER END AS MOD_IN_GENDER;

or like this: 或像这样:

gender LIKE COALESCE(IN_GENDER, '%')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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