微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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 
版权声明:本文为博主原创文章,转载请附上博文链接

 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐