Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
询问mysql中的存储函数_Mysql - Fatal编程技术网

询问mysql中的存储函数

询问mysql中的存储函数,mysql,Mysql,如果存在删除功能,请检查成员信息状态 分隔符$$ -- --功能 CREATE DEFINER=root@localhost函数rty\u check\u member\u info\u status(memb\u id int,field\u name\u 1 varchar(100),field\u name\u 2 varchar(100),login\u member\u amount int(11),login\u status char(1))返回char(1)CHARSET拉丁字符

如果存在删除功能,请检查成员信息状态

分隔符$$

--

--功能 CREATE DEFINER=
root
@
localhost
函数
rty\u check\u member\u info\u status
(memb\u id int,field\u name\u 1 varchar(100),field\u name\u 2 varchar(100),login\u member\u amount int(11),login\u status char(1))返回char(1)CHARSET拉丁字符集 开始 声明fn\u字段\u名称\u 1 varchar(100); 声明fn\u字段\u名称\u 2 varchar(100); 为可见整数(11)申报fn金额

结束$$
定界符

您有两种选择,一种是生成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)的副本如下:


我无法理解第一个答案。你能举个简单的例子吗?这很紧急。如果你能帮助我,我将非常感激你。
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
;
-- 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;