MySQL-多條件拼接語句


 

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;

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM