企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
### 传参条件: #### 一个参数 > 参数为字符时,直接拼入 SQL > 参数为数组时,数组的每一项分别作为条件执行where 查询(用法见本章节最后那个示例) #### 二个参数 > 二个参数分别表示:字段名, 值 > 如: where('sex', '女') > 等效SQL:WHERE sex = '女' #### 三个参数 > 三个参数分别表示:字段名, 操作符,值 > 如:where('age', '>=', 18); > 等效SQL: WHERE age >= 16 > 如:where('age', 'IN', [18,19,20]); > 等效SQL: WHERE age IN (18,19,20) > > 操作符可选值:=/<>/!=/>/</>=/<=/between/not between/in/not in/like/not like ### 用法预览 ~~~ $table->where('username','Tom'); $table->where('username','like','Tom'); $table->where('age','=',18); $table->where('age','>',18); $table->where('age','between', [18, 30]); $table->where('user_id','in', [1, 2, 3, 4]); $table->where('username LIKE \'Tom\''); $table->where('username LIKE ?', ['Tom']); $table->where('(')->where('username','like','Tom')->where('OR')->where('age','>',18)->where(')'); $table->where([ ['username','Tom'], 'OR', ['age','>',18], ]); // 最终SQL: WHERE (username='Tom' OR age>18) ~~~ ### 代码示例: #### 按某个字段的指定值查询 ~~~ $tableUser = Be::getTable('system_user'); $tableUser->where('sex', '女'); $users = $tableUser->getObjects(); ~~~ 等效SQL: ~~~ SELECT * FROM user WHERE sex = '女' ~~~ #### 按某个字段的指定范围查询 ~~~ $tableUser = Be::getTable('system_user'); $tableUser->where('age', '>=', 18); $users = $tableUser->getObjects(); ~~~ 等效SQL: ~~~ SELECT * FROM user WHERE age >= 18 ~~~ #### 多个条件 ~~~ $tableUser = Be::getTable('system_user'); $tableUser->where('sex', '女'); $tableUser->where('age', '>=', 18); $users = $tableUser->getObjects(); ~~~ 等效SQL: ~~~ SELECT * FROM user WHERE sex = '女' AND age >= 18 ~~~ #### 连贯操作 ~~~ $users = Be::getTable('system_user') ->where('sex', '女') ->where('age', '>=', 18) ->getObjects(); ~~~ #### 字段控制 ~~~ $users = Be::getTable('system_user') ->where('sex', '女') ->getObjects('id, name, sex'); ~~~ 等效SQL: ~~~ SELECT id, name, sex FROM user WHERE sex = '女' ~~~ #### 复杂条件: ~~~ $users = Be::getTable('system_user') ->where('(') ->where('sex', '女') ->where('AND') ->where('age', '>=', 18) ->where(')') ->where('OR') ->where('sex', '男') ->getObjects(); ~~~ 等效SQL: ~~~ SELECT * FROM user WHERE (sex = '女' AND age >= 18) OR sex = '男' ~~~ #### 数组形式的多维 where 条件 ~~~ $where = [ '(', ['sex', '女'], 'AND', ['age', '>=', 18], ')', 'OR', ['sex', '男'] ]; $users = Be::getTable('system_user') ->where($where) ->getObjects(); ~~~ 等效于上例中的复杂条件查询