From a924186ce6f58f5ab140362a92ede82b898cb211 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Sat, 12 Nov 2016 10:38:19 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E4=BF=AE=E6=AD=A3count=20avg=E6=96=B9?= =?UTF-8?q?=E6=B3=95=E4=BD=BF=E7=94=A8fetchsql=E6=97=A0=E6=B3=95=E6=AD=A3?= =?UTF-8?q?=E7=A1=AE=E8=BF=94=E5=9B=9Esql=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/db/Query.php | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/library/think/db/Query.php b/library/think/db/Query.php index 3f4d5fd0..fdf9d623 100644 --- a/library/think/db/Query.php +++ b/library/think/db/Query.php @@ -378,7 +378,7 @@ class Query public function value($field, $default = null) { $result = false; - if (!empty($this->options['cache'])) { + if (empty($options['fetch_sql']) && !empty($this->options['cache'])) { // 判断查询缓存 $cache = $this->options['cache']; if (empty($this->options['table'])) { @@ -422,7 +422,7 @@ class Query public function column($field, $key = '') { $result = false; - if (!empty($this->options['cache'])) { + if (empty($options['fetch_sql']) && !empty($this->options['cache'])) { // 判断查询缓存 $cache = $this->options['cache']; if (empty($this->options['table'])) { @@ -489,7 +489,8 @@ class Query */ public function count($field = '*') { - return (int) $this->value('COUNT(' . $field . ') AS tp_count', 0); + $value = $this->value('COUNT(' . $field . ') AS tp_count', 0); + return is_numeric($value) ? (int) $value : $value; } /** @@ -500,7 +501,8 @@ class Query */ public function sum($field = '*') { - return $this->value('SUM(' . $field . ') AS tp_sum', 0) + 0; + $value = $this->value('SUM(' . $field . ') AS tp_sum', 0); + return is_numeric($value) ? $value + 0 : $value; } /** @@ -535,7 +537,8 @@ class Query */ public function avg($field = '*') { - return $this->value('AVG(' . $field . ') AS tp_avg', 0) + 0; + $value = $this->value('AVG(' . $field . ') AS tp_avg', 0); + return is_numeric($value) ? $value + 0 : $value; } /** From 310775471a00f762bcf0ba6c2f92dd073dbb92cc Mon Sep 17 00:00:00 2001 From: thinkphp Date: Sat, 12 Nov 2016 11:11:18 +0800 Subject: [PATCH 2/3] =?UTF-8?q?table=E6=96=B9=E6=B3=95=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E5=AD=97=E7=AC=A6=E4=B8=B2=E6=96=B9=E5=BC=8F=E7=9A=84=E5=AD=90?= =?UTF-8?q?=E6=9F=A5=E8=AF=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/db/Query.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/library/think/db/Query.php b/library/think/db/Query.php index fdf9d623..5e7bf07d 100644 --- a/library/think/db/Query.php +++ b/library/think/db/Query.php @@ -1050,7 +1050,10 @@ class Query public function table($table) { if (is_string($table)) { - if (strpos($table, ',')) { + if (strpos($table, ')')) { + // 子查询 + $table = $table; + } elseif (strpos($table, ',')) { $tables = explode(',', $table); $table = []; foreach ($tables as $item) { From 72209e80353a5f34b2ad9d5863fd9d85e98dfc19 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Sat, 12 Nov 2016 23:55:17 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E6=94=B9=E8=BF=9BQuery=E7=B1=BB=E7=9A=84?= =?UTF-8?q?=E8=81=9A=E5=90=88=E6=9F=A5=E8=AF=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/db/Query.php | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/library/think/db/Query.php b/library/think/db/Query.php index 5e7bf07d..5a5597e1 100644 --- a/library/think/db/Query.php +++ b/library/think/db/Query.php @@ -373,9 +373,10 @@ class Query * @access public * @param string $field 字段名 * @param mixed $default 默认值 + * @param bool $force 强制转为数字类型 * @return mixed */ - public function value($field, $default = null) + public function value($field, $default = null, $force = false) { $result = false; if (empty($options['fetch_sql']) && !empty($this->options['cache'])) { @@ -397,6 +398,9 @@ class Query return $pdo; } $result = $pdo->fetchColumn(); + if ($force) { + $result = is_numeric($result) ? $result + 0 : $result; + } if (isset($cache)) { // 缓存数据 if (isset($cache['tag'])) { @@ -489,8 +493,7 @@ class Query */ public function count($field = '*') { - $value = $this->value('COUNT(' . $field . ') AS tp_count', 0); - return is_numeric($value) ? (int) $value : $value; + return $this->value('COUNT(' . $field . ') AS tp_count', 0, true); } /** @@ -501,8 +504,7 @@ class Query */ public function sum($field = '*') { - $value = $this->value('SUM(' . $field . ') AS tp_sum', 0); - return is_numeric($value) ? $value + 0 : $value; + return $this->value('SUM(' . $field . ') AS tp_sum', 0, true); } /** @@ -513,8 +515,7 @@ class Query */ public function min($field = '*') { - $value = $this->value('MIN(' . $field . ') AS tp_min', 0); - return is_numeric($value) ? $value + 0 : $value; + return $this->value('MIN(' . $field . ') AS tp_min', 0, true); } /** @@ -525,8 +526,7 @@ class Query */ public function max($field = '*') { - $value = $this->value('MAX(' . $field . ') AS tp_max', 0); - return is_numeric($value) ? $value + 0 : $value; + return $this->value('MAX(' . $field . ') AS tp_max', 0, true); } /** @@ -537,8 +537,7 @@ class Query */ public function avg($field = '*') { - $value = $this->value('AVG(' . $field . ') AS tp_avg', 0); - return is_numeric($value) ? $value + 0 : $value; + return $this->value('AVG(' . $field . ') AS tp_avg', 0, true); } /**