查询条件怎么写


等于

  1. User::where(99)->row();
  2. User::where('id', 99)->row();
  3. User::where('id', '=', 99)->row();
  4. User::where(['id' => 99])->row();
  5. User:find(99);
  6. //以上五个查询都是等效的,SQL: SELECT * FROM user WHERE id = 99

大于,小于,不等于

  1. User::where([
  2. 'level' => 8
  3. 'course_id' => ['>', 99],
  4. 'type' => ['<', 2],
  5. 'status' => ['<>', 0]
  6. ])->select();
  7. //SQL: SELECT * FROM WHERE level = 8 AND course_id > 99 AND type < 2 AND status <> 0

BETWEEN

  1. $where = [
  2. 'course_id' => ['between', 200, 300]
  3. ];
  4. //SQL: WHERE course_id BETWEEN 200 AND 300

LIKE

  1. $where = [
  2. 'name' => ['like', '%TimoPHP%']
  3. ];
  4. //SQL: WHERE name LIKE '%TimoPHP%'

IN

  1. User::where('id', 'in', [1, 3, 5, 7, 9])->select();
  2. User::where('id', 'in', '1,3,5,7,9')->select();
  3. //SQL: SELECT * FROM user WHERE id IN(1,3,5,7,9)

OR

  1. User::where(['type' => 1, 'sex' => 2])->orWhere(function(){
  2. 'level' => 8,
  3. 'is_vip' => 1
  4. })->select();
  5. //SQL: SELECT * FROM user WHERE (type = 1 AND sex = 2) OR (level = 8 AND is_vip = 1)

查询字符串

  1. User::where('_string', 'type = 1 or (level = 8 and is_vip = 1)')->select()
  2. //SQL: SELECT * FROM user WHERE type = 1 or (level = 8 and is_vip = 1);
  3. User::where([
  4. 'id' => ['>', 2],
  5. '_string' => 'sex = 2'
  6. ])->select();
  7. //SQL: SELECT * FROM user WHERE id > 2 AND sex = 2;