ThinkPHP 中 where條件 or,and 同時使用


('a'=1 and 'b'=2) or ('c'=3 and 'd'=4) and 'e'=5
$where_1['a'] = 1;
$where_1['b'] = 2;
$where_2['c'] = 3;
$where_2['d'] = 4;
$where_main['_complex'] = array(
    $where_1,
    $where_2,
    '_logic' => 'or'
);
$where_main['e'] = 5;
$this->user->where($where_main)->select();
SELECT * FROM `ecs_order_info` WHERE ( ( `order_status` = 5 AND `shipping_status` = 2 ) OR `order_status` = 6 ) AND `user_id` = 1

上面標紅的where查詢語句在TP中的拼接如下:

    $order=M('order_info');
    $where['order_status']=5;
    $where['shipping_status']=2;
    $map['_complex'] = $where;
    $map['order_status']=6;
    $map['_logic'] = 'or';  
    $final['_complex'] = $map;
    $final['user_id']=1;

   然后直接查詢就可以了: $order->where($final)->select();

   問題來了,怎么檢測我們寫的sql語句沒有錯呢?執行下面這條語句就可以了,只會翻譯成sql語句,不會執行。

   echo $order->where($final)->fetchSql(true)->select();
--------------------- 
作者:gerberasn 
來源:CSDN 
原文:https://blog.csdn.net/landylxy/article/details/70139841 
版權聲明:本文為博主原創文章,轉載請附上博文鏈接!

 


免責聲明!

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



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