mysql - 询问mysql中的存储函数

标签 mysql

如果存在则删除函数 rty_check_member_info_status;

分隔符 $$

--

-- 函数

CREATE DEFINER=root@localhost FUNCTION rty_check_member_info_status(memb_id int,field_name_1 varchar(100),field_name_2 varchar(100),login_member_amount int (11),login_status char(1)) RETURNS char(1) 字符集 latin1 开始 声明 fn_field_name_1 varchar(100) ; 声明 fn_field_name_2 varchar(100) ; 声明 fn_amount_for_profile_visible int(11);

declare fn_return char(1) default 'N';

declare test_field varchar(100);    

select field_name_1,field_name_2,amount_for_profile_visible into
fn_field_name_1,fn_field_name_2,fn_amount_for_profile_visible
from member_account_settings inner join tbl_members on member_account_settings.member_auto_id = tbl_members.member_id 
where tbl_members.member_id = memb_id  ;

if fn_field_name_1 = 'H' Then
   set fn_return = 'N' ;
else
if fn_field_name_2 = 'Y' Then
    if fn_amount_for_profile_visible = '0' Then
    set fn_return = 'Y' ;
    else
       if login_status = 1 Then
              if fn_amount_for_profile_visible > login_member_amount Then
              set fn_return = 'N' ;
              else
              set fn_return = 'Y' ; 
              end if;
       else 
       set fn_return = 'N';  
       end if ;  
    end if;    
else
set fn_return = 'Y';
end if ;
end if ;

return fn_return ;

结束$$ 分隔符;

最佳答案

你有两个选择几乎是生成的 SQL(通常是一个坏主意,因为它更难编写、调试和记录)和使用 case 语句根据匹配字符串的名称选择列(这通常是一个漂亮的好的解决方案)。

这是第二个示例,因为这是我绝对推荐的解决方案。

SET @test_field1 = "last_name_display_status" ;
SET @test_field2 = "last_name_display_for_other_partcpnt" ;

SELECT
     CASE @test_field1
        -- List columns here that you might want to return:
        WHEN 'last_name_display_status' THEN last_name_display_status
        WHEN 'last_name_display_for_other_partcpnt' THEN last_name_display_for_other_partcpnt
        WHEN 'create_date' THEN create_date
        -- Return a value for an invalid name here:
        ELSE NULL
     END AS test_field1,
     CASE @test_field2
        -- List columns here that you might want to return:
        WHEN 'last_name_display_status' THEN last_name_display_status
        WHEN 'last_name_display_for_other_partcpnt' THEN last_name_display_for_other_partcpnt
        WHEN 'create_date' THEN create_date
        -- Return a value for an invalid name here:
        ELSE NULL
     END AS test_field2,
     -- Rest of select unaffected by this change
     amount_for_profile_visible
   INTO
     fn_field_name_1,
     fn_field_name_2,
     fn_amount_for_profile_visible
FROM member_account_settings
INNER JOIN tbl_members
  ON member_account_settings.member_auto_id = tbl_members.member_id
WHERE
  tbl_members.member_id = memb_id
;

为了完整起见,我提出的第一个解决方案(生成的 SQL)的副本:

-- Need to use @vars, since named vars aren't in scope for the generated SQL:
SET @output1 = '';
SET @output2 = '';
SET @output3 = '';
SET @input1 = memb_id;

-- We also need to store our generated SQL to a variable
SET @query = 'SELECT ' + @test_field1 + ',' + @test_field2 + ', amount_for_profile_visible INTO @output1, @output2, @output3 FROM member_account_settings INNER JOIN tbl_members ON member_account_settings.member_auto_id = tbl_members.member_id WHERE tbl_members.member_id = ?';
-- To execute the code we have to convert it to a prepared statement
-- named stmt here, because it's what most people use in this instance
PREPARE stmt FROM @query;
-- Execute the statement using our input variable
EXECUTE stmt USING @input1;
-- Delete the prepared statement now we've run it.
DEALLOCATE PREPARE stmt;

-- Store our @vars back into the named vars.
SET fn_field_name_1 = @output1;
SET fn_field_name_2 = @output2;
SET fn_amount_for_profile_visible = @output3;

关于mysql - 询问mysql中的存储函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24403313/

相关文章:

php - 使用 joomla 外部 PHP 登录

c# - 如何为每种产品存储多种尺寸

MySQL内连接问题

MySQL:组合 2 个 JOINS

mysql - 适合浴室的 MySQL 列类型

java - 如何更改 netbeans 中不可编辑/生成的代码

php - 一次将值插入到多个 MySQL 表中

mysql - 在 Grails 中使用复合主键搭建 MySQL 表

mysql - TypeError : Router. use() 需要一个中间件函数但得到一个对象(NodeJs、Express 和 Mysql)

Mysql 触发器不工作。为什么?