码迷,mamicode.com
首页 > 数据库 > 详细

MySQL-多条件拼接语句

时间:2015-06-03 19:02:44      阅读:221      评论:0      收藏:0      [点我收藏+]

标签:

 

BEGIN
SET @baseStr= "SELECT * FROM tbName WHERE 1=1 ";
SET @_where="";
IF 1=1 THEN
  SET @_where= CONCAT(@_where," AND sourcedomain=\"www.baidu.com\" ");
END IF;

IF 2=2 THEN 
  SET @_where=CONCAT(@_where," AND userId =4444");
END IF;
  SET @sentence =CONCAT(@baseStr,@_where) ; -- 连接字符串生成要执行的SQL语句
  prepare stmt from @sentence; -- 预编释一下。 “stmt”预编释变量的名称,
  execute stmt; -- 执行SQL语句
  deallocate prepare stmt;    -- 释放资源
END

 

2.out赋值

SET @_rowCount=0; -- 变量
SET @_count =CONCAT("SELECT COUNT(*) INTO @_rowCount ",@baseStr,@_where);
prepare stmt from @_count;
execute stmt;
deallocate prepare stmt;
SET _pageCount=@_rowCount;

 

MySQL-多条件拼接语句

标签:

原文地址:http://www.cnblogs.com/liuph/p/4549760.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!