Home Database Mysql Tutorial MSSQL和Mysql 自定义函数与存储过程_MySQL

MSSQL和Mysql 自定义函数与存储过程_MySQL

Jun 01, 2016 pm 01:57 PM
mysql string

      mysql和mssql自定义都不带切割字符串的函数,例如在输入
(A,B,C),
我们需要得出一个列

数据
A
B
C


  这在系统自己种并不包含,需要自定义函数,先看看MSSQL的自定义函数:
CREATE    function    StrSplit(@c   varchar(2000),@split  varchar(2)=',')  
  returns   @t   table(col   varchar(100))  
  as  
    begin  
   
      while(charindex(@split,@c)0)  
        begin  
          insert   @t(col)   values   (substring(@c,1,charindex(@split,@c)-1))  
          set   @c   =   stuff(@c,1,charindex(@split,@c),'')  
        end  
      insert   @t(col)   values   (@c)  
      return  
    end

GO

 使用的时候
select * from  dbo.StrSplit('52,50,55',','))


即可。

   在MYSQL里面,我也希望能用自定义函数,但是,目前的版本并不支持返回table这种类型的函数,所以不得不考虑使用存储过程。
  首先,了解mysql创建临时表的语句:


set global log_bin_trust_function_creators = 1;
DELIMITER $$

DROP FUNCTION IF EXISTS `Myf`$$

CREATE DEFINER=`root`@`%` FUNCTION `Myf`() RETURNS varchar(50) CHARSET utf8
begin
return concat('-FUNCTION');
end$$

DELIMITER ;


然后看看使用:
SELECT Myf('MYF?');

然后是存储过程:

DELIMITER //

DROP PROCEDURE IF EXISTS split_string //
CREATE PROCEDURE split_string (
    IN input VARCHAR(2000)
    , IN `delimiter` VARCHAR(10)
)
SQL SECURITY INVOKER
COMMENT
'Splits a supplied string using using the given delimiter,
placing values in a temporary table'
BEGIN
    DECLARE cur_position INT DEFAULT 1 ;
    DECLARE remainder VARCHAR(2000);
    DECLARE cur_string VARCHAR(1000);
    DECLARE delimiter_length TINYINT UNSIGNED;
   
    DROP TEMPORARY TABLE IF EXISTS SplitValues;
    CREATE TEMPORARY TABLE SplitValues (
        value VARCHAR(1000) NOT NULL PRIMARY KEY
    ) ENGINE=MyISAM;
   
    SET remainder = input;
    SET delimiter_length = CHAR_LENGTH(delimiter);
   
    WHILE CHAR_LENGTH(remainder) > 0 AND cur_position > 0 DO
        SET cur_position = INSTR(remainder, `delimiter`);
        IF cur_position = 0 THEN
            SET cur_string = remainder;
        ELSE
            SET cur_string = LEFT(remainder, cur_position - 1);
        END IF;
        IF TRIM(cur_string) != '' THEN
            INSERT INTO SplitValues VALUES (cur_string);
        END IF;
        SET remainder = SUBSTRING(remainder, cur_position + delimiter_length);
    END WHILE;
   
END //

DELIMITER ;

/*
Tests
*/

CALL split_string('Steve,Jan,Bobby,Jay,Kaj,Colin',',');

SELECT * FROM SplitValues;

CREATE TEMPORARY TABLE TestJoin (join_field VARCHAR(20) PRIMARY KEY) ENGINE=MEMORY;

INSERT INTO TestJoin VALUES ('Kaj'),('Colin');

SELECT tj.* FROM TestJoin tj
INNER JOIN SplitValues sv
ON tj.join_field = sv.value;
//这个代码来自MYSQL官方,我没有验证

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP's big data structure processing skills PHP's big data structure processing skills May 08, 2024 am 10:24 AM

PHP's big data structure processing skills

How to optimize MySQL query performance in PHP? How to optimize MySQL query performance in PHP? Jun 03, 2024 pm 08:11 PM

How to optimize MySQL query performance in PHP?

How to use MySQL backup and restore in PHP? How to use MySQL backup and restore in PHP? Jun 03, 2024 pm 12:19 PM

How to use MySQL backup and restore in PHP?

What are the application scenarios of Java enumeration types in databases? What are the application scenarios of Java enumeration types in databases? May 05, 2024 am 09:06 AM

What are the application scenarios of Java enumeration types in databases?

How to insert data into a MySQL table using PHP? How to insert data into a MySQL table using PHP? Jun 02, 2024 pm 02:26 PM

How to insert data into a MySQL table using PHP?

How to fix mysql_native_password not loaded errors on MySQL 8.4 How to fix mysql_native_password not loaded errors on MySQL 8.4 Dec 09, 2024 am 11:42 AM

How to fix mysql_native_password not loaded errors on MySQL 8.4

How to use MySQL stored procedures in PHP? How to use MySQL stored procedures in PHP? Jun 02, 2024 pm 02:13 PM

How to use MySQL stored procedures in PHP?

Performance optimization strategies for PHP array paging Performance optimization strategies for PHP array paging May 02, 2024 am 09:27 AM

Performance optimization strategies for PHP array paging

See all articles