查询条件怎么写


等于

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

大于,小于,不等于

User::where([
    'level' => 8
    'course_id' => ['>', 99],
    'type' => ['<', 2],
    'status' => ['<>', 0]
])->select();

//SQL: SELECT * FROM WHERE level = 8 AND course_id > 99 AND type < 2 AND status <> 0

BETWEEN

$where = [
    'course_id' => ['between', 200, 300]
];

//SQL: WHERE course_id BETWEEN 200 AND 300

LIKE

$where = [
    'name' => ['like', '%TimoPHP%']
];

//SQL: WHERE name LIKE '%TimoPHP%'

IN

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

OR

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

查询字符串

User::where('_string', 'type = 1 or (level = 8 and is_vip = 1)')->select()
//SQL: SELECT * FROM user WHERE type = 1 or (level = 8 and is_vip = 1);

User::where([
    'id' => ['>', 2],
    '_string' => 'sex = 2'
])->select();

//SQL: SELECT * FROM user WHERE id > 2 AND sex = 2;