Thinkphp 查詢條件 and 和 or同時使用即復合查詢


Thinkphp 查詢條件 and 和 or同時使用即復合查詢

開發過程中經常會遇到查詢條件and 和or都同時使用的情況,Thinkphp中本來就支持這個查詢的,不一定非得自己寫sql語句,使用復合查詢即可:

三、復合查詢
復合查詢相當於封裝了一個新的查詢條件,然后並入原來的查詢條件之中,所以可以完成比較復雜的查詢條件組裝。
例如:

  1. $where[‘name’]  = array(‘like’, ‘%thinkphp%’);
  2. $where[‘title’]  = array(‘like’,’%thinkphp%’);
  3. $where[‘_logic’] = ‘or’;
  4. $map[‘_complex’] = $where;
  5. $map[‘id’]  = array(‘gt’,1);

查詢條件是
(id>1)AND( (namelike’%thinkphp%’)OR(titlelike’%thinkphp%’) )
復合查詢使用了_complex作為子查詢條件來定義,配合之前的查詢方式,可以非常靈活的制定更加復雜的查詢條件。
很多查詢方式可以相互轉換,例如上面的查詢條件可以改成:

  1. $where[‘id’] = array(‘gt’,1);
  2. $where[‘_string’] = ‘ (name like “%thinkphp%”)  OR ( title like “%thinkphp”) ‘;

最后生成的SQL語句是一致的。


免責聲明!

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



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