查询语言.where

函数原型

  1. public function where($mixCond /* args */);

字段 (表达式) 值

  1. ## SELECT `test`.* FROM `test` WHERE `test`.`id` = 1
  2. Db::table('test')->
  3. where('id', '=', 1)->
  4. getAll();
  5. ## SELECT `test`.* FROM `test` WHERE `test`.`id` = 2
  6. Db::table('test')->
  7. where('id', 2)->
  8. getAll();
  9. ## SELECT `test`.* FROM `test` WHERE `test`.`id` = 2 AND `test`.`name` > '狗蛋' AND `test`.`value` LIKE '小鸭子'
  10. Db::table('test')->
  11. where('id', 2)->
  12. where('name', '>', '狗蛋')->
  13. where('value', 'like', '小鸭子')->
  14. getAll();

以数组直接传入

  1. ## SELECT `test`.* FROM `test` WHERE `test`.`name` LIKE '技术'
  2. Db::table('test')->
  3. where(['name','like', '技术'])->
  4. getAll();
  5. ## SELECT `test`.* FROM `test` WHERE `test`.`name` LIKE '技术' AND `test`.`value` <> '结局'
  6. Db::table('test')->
  7. where ([
  8. ['name','like', '技术'],
  9. ['value','<>', '结局']
  10. ])->
  11. getAll();

OrWhere 语法

  1. ## SELECT `test`.* FROM `test` WHERE `test`.`name` LIKE '技术'
  2. Db::table('test')->
  3. where('name','like', '技术')->
  4. orWhere('value','<>', '结局')->
  5. getAll();

whereBetween 语法

  1. ## SELECT `test`.* FROM `test` WHERE `test`.`id` BETWEEN 1 AND 100
  2. Db::table('test')->
  3. whereBetween('id', [1, 100])->
  4. getAll();
  5. ## SELECT `test`.* FROM `test` WHERE `test`.`id` BETWEEN 1 AND 10
  6. Db::table('test')->
  7. where('id','between', [1, 10])->
  8. getAll();

whereBetween 二维数组语法支持

后面的语法均支持这样的写法,后面不再继续示例了。

  1. ## SELECT `test`.* FROM `test` WHERE `test`.`id` BETWEEN 1 AND 100 AND `test`.`name` BETWEEN 5 AND 22
  2. Db::table('test')->
  3. whereBetween([
  4. ['id', [1, 100]],
  5. ['name', [5, 22]]
  6. ])->
  7. getAll();

whereNotBetween 语法

  1. ## SELECT `test`.* FROM `test` WHERE `test`.`id` NOT BETWEEN 1 AND 10
  2. Db::table('test')->
  3. whereNotBetween('id', [1, 10])->
  4. getAll();
  5. ## SELECT `test`.* FROM `test` WHERE `test`.`id` NOT BETWEEN 1 AND 10
  6. Db::table('test')->
  7. where('id','not between', [1, 10])->
  8. getAll();

whereIn 语法

  1. ## SELECT `test`.* FROM `test` WHERE `test`.`id` IN (2, 50)
  2. Db::table('test')->
  3. whereIn('id', [2, 50])->
  4. getAll();
  5. ## SELECT `test`.* FROM `test` WHERE `test`.`id` IN ('1','10')
  6. Db::table('test')->
  7. where('id', 'in', '1,10')
  8. ->getAll();
  9. ## SELECT `test`.* FROM `test` WHERE `test`.`id` IN (2,50)
  10. Db::table('test')->
  11. where('id', 'in', [2, 50])
  12. ->getAll();

whereNotIn 语法

  1. ## SELECT `test`.* FROM `test` WHERE `test`.`id` NOT IN (2,50)
  2. Db::table('test')->
  3. whereNotIn('id', [2, 50])->
  4. getAll();
  5. ## SELECT `test`.* FROM `test` WHERE `test`.`id` NOT IN ('1','10')
  6. Db::table('test')->
  7. where('id','not in', '1,10')->
  8. getAll();

whereNull 语法

  1. ## SELECT `test`.* FROM `test` WHERE `test`.`id` IS NULL
  2. Db::table('test')->
  3. whereNull('id')->
  4. getAll();
  5. ## SELECT `test`.* FROM `test` WHERE `test`.`id` IS NULL
  6. Db::table('test')->
  7. where('id','null')->
  8. getAll();

whereNotNull 语法

  1. ## SELECT `test`.* FROM `test` WHERE `test`.`id` IS NOT NULL
  2. Db::table('test')->
  3. whereNotNull('id')->
  4. getAll();
  5. ## SELECT `test`.* FROM `test` WHERE `test`.`id` IS NOT NULL
  6. Db::table('test')->
  7. where('id','not null')->
  8. getAll();

whereLike 语法

  1. ## SELECT `test`.* FROM `test` WHERE `test`.`id` LIKE '5'
  2. Db::table('test')->
  3. whereLike('id','5')->
  4. getAll();
  5. ## SELECT `test`.* FROM `test` WHERE `test`.`id` LIKE '5'
  6. Db::table('test')->
  7. where('id','like', '5')->
  8. getAll();

whereNotLike 语法

  1. ## SELECT `test`.* FROM `test` WHERE `test`.`id` NOT LIKE '5'
  2. Db::table('test')->
  3. whereNotLike('id','5')->
  4. getAll();
  5. ## SELECT `test`.* FROM `test` WHERE `test`.`id` NOT LIKE '5'
  6. Db::table('test')->
  7. where('id','not like', '5')->
  8. getAll();

whereExists 语法

  1. ## SELECT `test`.* FROM `test` WHERE EXISTS (SELECT `subsql`.* FROM `subsql` WHERE `subsql`.`id` = 1)
  2. Db::table('test')->
  3. whereExists(
  4. function($select) {
  5. $select->table('subsql')->where('id', 1);
  6. }
  7. )->
  8. getAll();
  9. ## SELECT `test`.* FROM `test` WHERE EXISTS (SELECT `subsql`.* FROM `subsql`)
  10. $subSelect = Db::table('subsql');
  11. Db::table('test')->
  12. where(
  13. [
  14. 'exists__' => $subSelect
  15. ]
  16. )->
  17. getAll();
  18. ## SELECT `test`.* FROM `test` WHERE EXISTS (select *from d_sub)
  19. Db::table('test')->
  20. where(
  21. [
  22. 'exists__' => 'select *from d_sub'
  23. ]
  24. )->
  25. getAll();
  26. ## SELECT `test`.* FROM `test` WHERE EXISTS (SELECT `subsql`.* FROM `subsql` WHERE `subsql`.`id` = 1)
  27. Db::table('test')->
  28. where(
  29. [
  30. 'exists__' => function($select) {
  31. $select->table('subsql')->where('id', 1);
  32. }
  33. ]
  34. )->
  35. getAll();

whereNotExists 语法

  1. ## SELECT `test`.* FROM `test` WHERE NOT EXISTS ( SELECT `subsql`.* FROM `subsql` WHERE `subsql`.`id` = 1 )
  2. Db::table('test')->
  3. whereNotExists(
  4. function($select){
  5. $select->table('subsql')->where('id', 1);
  6. }
  7. )->
  8. getAll();

whereDate、whereMonth、whereDay、whereYear 语法

本功能比较独立,剔除来了见《数据库构造器时间运算符 time endTime》

分组查询

  1. ## SELECT `test`.* FROM `test` WHERE `test`.`id` = '5' OR (`test`.`votes` > 100 AND `test`.`title` <> 'Admin')
  2. Db::table('test')->
  3. where('id', 5)->
  4. orWhere(function ($select) {
  5. $select->where('votes', '>', 100)->where('title', '<>', 'Admin');
  6. })->
  7. getAll();
  8. ## SELECT `test`.* FROM `test` WHERE `test`.`id` = '5' OR `test`.`name` = '小牛' AND (`test`.`votes` > 100 OR `test`.`title` <> 'Admin')
  9. Db::table('test')->
  10. where('id', 5)->
  11. orWhere('name', '小牛')->
  12. where(function ($select) {
  13. $select->where('votes', '>', 100)->orWhere('title', '<>', 'Admin');
  14. })->
  15. getAll();

条件表达式支持

条件表达式由 “{}” 包起来表示支持条件表达式,“[]” 表示支持字段格式化。

  1. ## SELECT `test`.`post`,`test`.`value`,concat("tt_",`test`.`id`) FROM `test` WHERE concat("hello_",`test`.`posts`) = `test`.`id`
  2. Db::table('test', 'post,value,{concat("tt_",[id])}')->
  3. where('{concat("hello_",[posts])}', '=', '{[id]}')->
  4. getAll();

字段作为键值

  1. ## SELECT `test`.* FROM `test` WHERE `test`.`id` = '故事' AND `test`.`name` IN (1,2,3) AND `test`.`weidao` BETWEEN '40' AND '100' AND `test`.`value` IS NULL AND `test`.`remark` IS NOT NULL AND `test`.`goods` = '东亚商品' AND `test`.`hello` = 'world'
  2. Db::table('test')->
  3. where([
  4. 'id' => ['=', '故事'],
  5. 'name' => ['in', [1,2,3]],
  6. 'weidao' => ['between', '40,100'],
  7. 'value' => 'null',
  8. 'remark' => ['not null'],
  9. 'goods' => '东亚商品',
  10. 'hello' => ['world']
  11. ])->
  12. getAll();

string__ 支持(支持字段)

  1. ## SELECT `test`.* FROM `test` WHERE `test`.`name` = 11 and `post`.`value` = 22 and concat("tt_",`test`.`id`)
  2. Db::table('test')->
  3. where (
  4. ['string__' => '{[name] = 11 and [post.value] = 22 and concat("tt_",[id])}']
  5. )->
  6. getAll();

subor 和 suband 支持

  1. ## SELECT `test`.* FROM `test` WHERE `test`.`hello` = 'world' OR (`test`.`id` LIKE '你好')
  2. Db::table('test')->
  3. where (
  4. [
  5. 'hello' => 'world',
  6. 'subor__' => ['id', 'like', '你好']
  7. ]
  8. )->
  9. getAll();
  10. ## SELECT `test`.* FROM `test` WHERE `test`.`hello` = '111' OR (`test`.`id` LIKE '你好' AND `test`.`value` = 'helloworld') AND (`test`.`id` LIKE '你好' OR `test`.`value` = 'helloworld' OR (`test`.`child_one` > '123' AND `test`.`child_two` LIKE '123'))
  11. Db::table('test')->
  12. where (
  13. [
  14. 'hello' => '111',
  15. 'subor__' => [
  16. ['id', 'like', '你好'],
  17. ['value', '=', 'helloworld']
  18. ],
  19. 'suband__' => [
  20. 'logic__' => 'or',
  21. ['id2', 'like', '你好2'],
  22. ['value2', '=', 'helloworld2'],
  23. 'subor__' => [
  24. ['child_one', '>', '123'],
  25. ['child_two', 'like', '123'],
  26. ]
  27. ]
  28. ]
  29. )->
  30. getAll();