首页 > 数据库技术 > 详细

MySQL-多条件拼接语句

时间:2015-06-03 19:01:29      阅读:367      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!