AggregateQuery.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2019 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types = 1);
  12. namespace think\db\concern;
  13. use think\db\Raw;
  14. /**
  15. * 聚合查询
  16. */
  17. trait AggregateQuery
  18. {
  19. /**
  20. * 聚合查询
  21. * @access protected
  22. * @param string $aggregate 聚合方法
  23. * @param string|Raw $field 字段名
  24. * @param bool $force 强制转为数字类型
  25. * @return mixed
  26. */
  27. protected function aggregate(string $aggregate, $field, bool $force = false)
  28. {
  29. return $this->connection->aggregate($this, $aggregate, $field, $force);
  30. }
  31. /**
  32. * COUNT查询
  33. * @access public
  34. * @param string|Raw $field 字段名
  35. * @return int
  36. */
  37. public function count(string $field = '*'): int
  38. {
  39. if (!empty($this->options['group'])) {
  40. // 支持GROUP
  41. $options = $this->getOptions();
  42. $subSql = $this->options($options)
  43. ->field('count(' . $field . ') AS think_count')
  44. ->bind($this->bind)
  45. ->buildSql();
  46. $query = $this->newQuery()->table([$subSql => '_group_count_']);
  47. $count = $query->aggregate('COUNT', '*');
  48. } else {
  49. $count = $this->aggregate('COUNT', $field);
  50. }
  51. return (int) $count;
  52. }
  53. /**
  54. * SUM查询
  55. * @access public
  56. * @param string|Raw $field 字段名
  57. * @return float
  58. */
  59. public function sum($field): float
  60. {
  61. return $this->aggregate('SUM', $field, true);
  62. }
  63. /**
  64. * MIN查询
  65. * @access public
  66. * @param string|Raw $field 字段名
  67. * @param bool $force 强制转为数字类型
  68. * @return mixed
  69. */
  70. public function min($field, bool $force = true)
  71. {
  72. return $this->aggregate('MIN', $field, $force);
  73. }
  74. /**
  75. * MAX查询
  76. * @access public
  77. * @param string|Raw $field 字段名
  78. * @param bool $force 强制转为数字类型
  79. * @return mixed
  80. */
  81. public function max($field, bool $force = true)
  82. {
  83. return $this->aggregate('MAX', $field, $force);
  84. }
  85. /**
  86. * AVG查询
  87. * @access public
  88. * @param string|Raw $field 字段名
  89. * @return float
  90. */
  91. public function avg($field): float
  92. {
  93. return $this->aggregate('AVG', $field, true);
  94. }
  95. }