查询语言.having

函数原型

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

having 和 where 用法一致,只是没有 where exists 这种写法。

字段 (表达式) 值

  1. # SELECT `test`.`tid` AS `id`,`test`.`tname` AS `value` FROM `test` GROUP BY `test`.`tid` HAVING `test`.`tid` > 5
  2. Db::table('test', 'tid as id,tname as value')->
  3. groupBy('tid')->
  4. having('tid', '>', 5)->
  5. getAll();

以数组直接传入

  1. # SELECT `test`.`name` AS `id`,`test`.`tname` AS `value` FROM `test` GROUP BY `test`.`name` HAVING `test`.`name` LIKE '技术'
  2. Db::table('test', 'name as id,tname as value')->
  3. groupBy('name')->
  4. having(['name', 'like', '技术'])->
  5. getAll();

OrHaving 语法

  1. # SELECT `test`.`name` AS `id`,`test`.`tname` AS `value` FROM `test` GROUP BY `test`.`name` HAVING `test`.`name` LIKE '技术' OR `test`.`tname` LIKE '技术'
  2. Db::table('test', 'name as id,tname as value')->
  3. groupBy('name')->
  4. having(['name', 'like', '技术'])->
  5. orHaving(['tname', 'like', '技术'])->
  6. getAll();

havingBetween 语法

  1. # SELECT `test`.`name` AS `id`,`test`.`tname` AS `value` FROM `test` GROUP BY `test`.`name` HAVING `test`.`id` BETWEEN 1 AND 10 AND `test`.`id` BETWEEN 1 AND 100
  2. Db::table('test', 'name as id,tname as value')->
  3. groupBy('name')->
  4. having('id', 'between', [1, 10])->
  5. havingBetween('id', [1, 100])->
  6. getAll();

havingBetween 二维数组语法支持

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

  1. # SELECT `test`.`name` AS `id`,`test`.`tname` AS `value` FROM `test` GROUP BY `test`.`name` HAVING `test`.`name` BETWEEN 1 AND 100 AND `test`.`tname` BETWEEN 5 AND 22
  2. Db::table('test', 'name as id,tname as value')->
  3. groupBy('name')->
  4. havingBetween([
  5. ['name', [1, 100]],
  6. ['tname', [5, 22]]
  7. ])->
  8. getAll();

havingNotBetween 语法

  1. # SELECT `test`.* FROM `test` GROUP BY `test`.`name` HAVING `test`.`id` NOT BETWEEN 1 AND 10 AND `test`.`id` NOT BETWEEN 1 AND 100
  2. Db::table('test')->
  3. groupBy('name')->
  4. having('id', 'not between', [1, 10])->
  5. havingNotBetween('id', [1, 100])->
  6. getAll();

havingIn 语法

  1. # SELECT `test`.* FROM `test` GROUP BY `test`.`name` HAVING `test`.`id` IN (2,50) AND `test`.`num` IN (2,50)
  2. Db::table('test')->
  3. groupBy('name')->
  4. having('id', 'in', [2, 50])->
  5. havingIn('num', [2, 50])->
  6. getAll();

havingNotIn 语法

  1. # SELECT `test`.* FROM `test` GROUP BY `test`.`name` HAVING `test`.`id` NOT IN (2,50) AND `test`.`num` NOT IN (2,50)
  2. Db::table('test')->
  3. groupBy('name')->
  4. having('id', 'not in', [2, 50])->
  5. havingNotIn('num', [2, 50])->
  6. getAll();

havingNull 语法

  1. # SELECT `test`.* FROM `test` GROUP BY `test`.`name` HAVING `test`.`id` IS NULL AND `test`.`num` IS NULL
  2. Db::table('test')->
  3. groupBy('name')->
  4. having('id', 'null')->
  5. havingNull('num')->
  6. getAll();

havingNotNull 语法

  1. # SELECT `test`.* FROM `test` GROUP BY `test`.`name` HAVING `test`.`id` IS NOT NULL AND `test`.`num` IS NOT NULL
  2. Db::table('test')->
  3. groupBy('name')->
  4. having('id', 'not null')->
  5. havingNotNull('num')->
  6. getAll();

havingLike 语法

  1. # SELECT `test`.* FROM `test` GROUP BY `test`.`name` HAVING `test`.`id` LIKE '123' AND `test`.`num` LIKE '55'
  2. Db::table('test')->
  3. groupBy('name')->
  4. having('id', 'like', '123')->
  5. havingLike('num', '55')->
  6. getAll();

havingNotLike 语法

  1. # SELECT `test`.* FROM `test` GROUP BY `test`.`name` HAVING `test`.`id` NOT LIKE '123' AND `test`.`num` NOT LIKE '55'
  2. Db::table('test')->
  3. groupBy('name')->
  4. having('id', 'not like', '123')->
  5. havingNotLike('num', '55')->
  6. getAll();

havingDate、havingMonth、havingDay、havingYear 语法

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

分组查询

  1. # SELECT `test`.* FROM `test` GROUP BY `test`.`id` HAVING `test`.`id` = 5 OR (`test`.`votes` > 100 AND `test`.`title` <> 'Admin')
  2. Db::table('test')->
  3. groupBy('id')->
  4. having('id', 5)->
  5. orHaving(function ($select) {
  6. $select->having('votes', '>', 100)->having('title', '<>', 'Admin');
  7. })->
  8. getAll();

条件表达式支持

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

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

字段作为键值

  1. # SELECT `test`.* FROM `test` GROUP BY `test`.`id` HAVING `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. groupBy('id')->
  4. having([
  5. 'id' => ['=','故事'],
  6. 'name' => ['in', [1, 2, 3]],
  7. 'weidao' => ['between', '40,100'],
  8. 'value' => 'null',
  9. 'remark' => ['not null'],
  10. 'goods' => '东亚商品',
  11. 'hello' => ['world']
  12. ])->
  13. getAll();

string__ 支持(支持字段)

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

subor 和 suband 支持

  1. # SELECT `test`.* FROM `test` GROUP BY `test`.`id` HAVING `test`.`hello` = 'world' OR (`test`.`id` LIKE '你好')
  2. Db::table('test')->
  3. groupBy('id')->
  4. having(
  5. [
  6. 'hello' => 'world',
  7. 'subor__' => ['id', 'like', '你好']
  8. ]
  9. )->
  10. getAll();
  11. # SELECT `test`.* FROM `test` GROUP BY `test`.`id` HAVING `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'))
  12. Db::table('test')->
  13. groupBy('id')->
  14. having(
  15. [
  16. 'hello' => '111',
  17. 'subor__' => [
  18. ['id', 'like', '你好'],
  19. ['value', '=', 'helloworld']
  20. ],
  21. 'suband__' => [
  22. 'logic__' => 'or',
  23. ['id', 'like', '你好'],
  24. ['value', '=', 'helloworld'],
  25. 'subor__' => [
  26. ['child_one', '>', '123'],
  27. ['child_two', 'like', '123'],
  28. ]
  29. ]
  30. ]
  31. )->
  32. getAll();