Home > Database > Mysql Tutorial > body text

MySQL执行拼接字符串语句实例

WBOY
Release: 2016-06-07 16:46:09
Original
2206 people have browsed it

-- 以下是一个MySQL执行拼接字符串语句实例:-- 为需要拼接的变量赋值SET @VARNAME=

-- 以下是一个MySQL执行拼接字符串语句实例:

-- 为需要拼接的变量赋值

SET @VARNAME='李';

-- 拼接字符串,其中?是执行拼接字符串语句的参数,@TestName是结果值

SET @SQLStr0=CONCAT('SELECT TestName INTO @TestName FROM test.t_TestTable WHERE Test_ID>? AND TestName LIKE "%',@VARNAME,'%" LIMIT 1;');

-- 预处理拼接好的字符串

PREPARE SQLStr1 FROM @SQLStr0;

-- 为参数赋值

SET @Test_ID=1;

-- 使用参数执行拼接好的字符串语句

EXECUTE SQLStr1 USING @Test_ID;

-- 释放拼接的字符串语句

DEALLOCATE PREPARE SQLStr1;

SELECT @TestName;-- 获取结果值

-- 如果不需要拼接变量或者直接都使用参数,那么可以直接开始预处理

-- 预处理拼接好的字符串

PREPARE SQLStr1 FROM 'SELECT TestName INTO @TestName FROM test.t_TestTable WHERE Test_ID>? AND TestName LIKE "%?%" LIMIT 1;';

-- 为参数赋值

SET @Test_ID=1;

SET @VARNAME='李';

-- 使用参数执行拼接好的字符串语句

EXECUTE SQLStr1 USING @Test_ID,@VARNAME;

-- 释放拼接的字符串语句

DEALLOCATE PREPARE SQLStr1;

SELECT @TestName;-- 获取结果值

linux

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!