zoukankan      html  css  js  c++  java
  • thinkphp3.2 where 条件查询 复查的查询语句

    复查的查询语句

    有的时候,我们希望通过一次的查询就能解决问题,这个时候查询条件往往比较复杂,但是却比多次查询库来的高效。

    实在是搞不定的话就直接用$where[‘_string’] = ‘xxxx’, 这个代表查询的时候拼接上 xxx 条件,一次性解决问题

    $where[‘_string’] = ‘left join A on A.id = b.id where a.id not in (select id from C)’;

    1. 区间查询(一个值得多种情况)
    默认是 and
    $where['id'] =array(array('neq','8'),array('elt','200'),'and'); // 小于等于200 不等于 8
    
    $where['id']  = array(array('neq','8'),'array('neq','10')','or'); // 不等于8或者不等于10
    1. 复合查询
      相当于封装了新的查询条件在里面
    $where['a'] = 5;
    $where['b'] = 6;
    $where['_logic'] = 'or'; 
    
    sql:where a = 5 or b = 6;
    
    $condition['c'] = '3';
    $condition['d'] = '4'
    $condition['_logic'] = 'or'
    $where['a'] = 9;
    $where['_complex'] = $condition;
    
    sql: where a=9 and (c = 3 or d = 4)
    
    根据需求,灵活使用(无限套下去)

    3.sql 查询

    如果有设置了读写分离的话 query 是查询 execute是更新保存

    M()->query(‘select * from a’);
    M()->execute(‘update a set counts = 3 where id = 1103’)

    4.获取要执行的sql 语句

    有的时候条件太复杂,比如 id in(xxxxx),这个xxx就是通过一系列操作获得的结果,嫌麻烦的就直接 都扔进去,写sql 又长,就直接获取sql语句扔进去

    1.fetchsql
    2.buildsql
    3.select(false)
    
    M('user')->fetchsql(true)->select();
    M('user')->buildsql();
    M('user')->select(false);
  • 相关阅读:
    Uncaught TypeError: Illegal invocation
    数组循环
    二维码
    验证码
    user_agent
    ip
    jquery操作dom
    php 正则
    hdu 4850 Wow! Such String! 欧拉回路
    leetcode Maximal Rectangle 单调栈
  • 原文地址:https://www.cnblogs.com/alexguoyihao/p/9399790.html
Copyright © 2011-2022 走看看